- this
//this:指的是类的实例
this的用法:
1、当成员变量和局部变量同名时,如果想调用成员变量,用this
2、当构造方法互相调用时,用this
string name;
int age;
double salary;
public Worker():this("杜成", 19, 6000)
{
//name = "袁化彬";
//age = 18;
//salary = 5000;
//Console.WriteLine(name + "\t" + age + "\t" + salary);
}
//public Worker(string name, int age, double salary):this()
public Worker(string name, int age, double salary)
{
this.name=name;
this.age = age;
this.salary = salary;
Console.WriteLine(name + "\t" + age + "\t" + salary);
}
同一个类中的多个方法可以有相同的方法名称,但是有不同的参数列表,这就称为方法重载
class Test10
{ //系统默认值
int a;//0
float b;//0.0
string c;//null
double d;//0.0
char e;// \u
bool f;//false
public Test10()//构造方法
{
Console.WriteLine("构造方法");
Console.WriteLine("a="+a+"\tb="+b+"\tc="+c);
Console.WriteLine("d=" + d + "\te=" + e + "\tf=" + f);
}
public Test10(double x)//构造方法
{
Console.WriteLine("参数值是:" + x);
}
public Test10(int x)//构造方法
{
Console.WriteLine("有参构造方法,参数值是:" + x);
}
//static void Main(string[] args)//作用:调用属性和方法
//{
// //Test10 t1 = new Test10();
// Test10 t2 = new Test10(10.2);
// //如果没用写构造方法 系统会默认提供一个无参的构造方法,方法里面是空的
// //如果写 会覆盖系统默认的
// Console.ReadKey();
//}
方法的重载的规则:
- 方法名称必须相同。
- 参数列表必须不同。
- 方法的返回类型可以相同也可以不相同。
- 仅仅返回类型不同不足以称为方法的重载。
- 对象的引用
class Test14
{
int a = 1;
void Get()
{
Console.WriteLine(a);
}
void Get2(Test14 t)//第三种引用
{
t.a++;
}
Test14 Get3(Test14 t)//第四种引用
{
t.a++;
return t;
}
static void Main(string[] args)
{
Test14 t = new Test14();
t.a = 2;//第一种引用
Test14 t2 = t;//第二种引用
//Console.WriteLine(t.a+"\t"+t2.a);// 2 2
//t2.Get();
//t.Get2(t);
//Console.WriteLine(t.a + "\t" + t2.a);// 3 3
Test14 t3 = t.Get3(t);
t3.a++;
Console.WriteLine(t.a + "\t" + t3.a);// 4 4
}
}