edw*_*ard 1 .net c# inheritance constructor
是什么区别this
和base
关键功能在下面的语句?
public Customer(string name, string referrerName) : base(name)
public Customer(string Name) : this(Name)
Run Code Online (Sandbox Code Playgroud)
base(name)
将使用提供的参数调用父类构造函数
this(name)
将使用提供的参数调用当前类构造函数,在您的情况下,该参数是当前构造函数并为您提供编译错误,因为构造函数无法调用自身.
假设你有这些课程
public class A
{
public A(string a) { Console.WriteLine(a); }
public A(int a) { Console.WriteLine(a * a); }
}
public class B : A
{
public B(string a): base (a) { }
public B(int a): this (a.ToString()) { }
}
Run Code Online (Sandbox Code Playgroud)
new B("hello")
将public A(string a)
在输出中调用并打印"hello"
new B(4)
将调用public B(string a)
哪个将调用public A(string a)
并在输出中打印"4"
归档时间: |
|
查看次数: |
68 次 |
最近记录: |