[求助]咨询关于this的问题
感谢各位斑友对于我这个初级编程的帮助,我今天又有个问题要问大家了(貌似每日一题,汗)1.是关于this用法的,具体就是我不知道this是干吗的,我查了msdn
using System;
public class Employee
{
public string name;
public string alias;
public decimal salary = 3000.00m;
// Constructor:
public Employee(string name, string alias)
{
// Use this to qualify the fields, name and alias:
this.name = name;
this.alias = alias;
}
// Printing method:
public void printEmployee()
{
Console.WriteLine("Name: {0}\nAlias: {1}", name, alias);
// Passing the object to the CalcTax method by using this:
Console.WriteLine("Taxes: {0:C}", Tax.CalcTax(this));
}
}
public class Tax
{
public static decimal CalcTax(Employee E)
{
return (0.08m*(E.salary));
}
}
public class MainClass
{
public static void Main()
{
// Create objects:
Employee E1 = new Employee ("John M. Trainer", "jtrainer");
// Display results:
E1.printEmployee();
}
}
The this keyword can be used to access members from within constructors, instance methods, and instance accessors.
我个人的理解就是this是用来访问构造函数,实例方法的,虽然我还不知道构造函数和实力方法是干嘛用的。。。然后this的限定功能是干吗的呢?
我看得懂,但是我的疑问就是为什么要用this,因为我觉得不用this也可以写出这个程序,请各位提点一下,谢谢。
2。关于成员访问的这个概念,老实说我现在很糊涂,这个概念是用在哪里的?
嗬嗬,对于我这个初级人员,这些概念问题很混乱,希望大家帮忙,谢谢