0%

C#this,base

this

原始类型扩展方法
* 静态方法
* 第一个参数作用类型
* using引用命名空间

​ static public GameObject AddChildNew(this GameObject parent) { return AddChild(parent, true); }

索引器

#####显式调用一个类的方法和成员 略

串联构造函数 <=> 对比Base
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Test
{
public Test()
{
Console.WriteLine("无参构造函数");
}
// 这里的this()指向的是Test()无参构造函数
// 相当于继承了无参构造函数
public Test(string text) : this()
{
// 程序进来后会先执行Test()无参函数,然后继续往下边执行
Console.WriteLine(text);
Console.WriteLine("有参构造函数");
}
}

轮子

####Base

在派生类中调用基类的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class A
{
public virtual void Hello()
{
Console.WiriteLine("Hello");
}
}
public class B : A
{
public override void Hello()
{
base.Hello();//调用基类的方法,显示Hello
Console.WiriteLine("World");
}
}
调用基类构造函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class A
{
public A()
{
Console.WriteLine("Build A");
}
}
public class B:A
{
public B():base()
{
Console.WriteLine("Build B");
}
static void Main()
{
B b = new B();
Console.ReadLine();
}

区别 - 构造

修饰构造时,所代表的构造方法:一个时来自 父类base,一个是来自于当前类this。