【发布时间】:2017-07-06 15:40:28
【问题描述】:
以下代码通过从派生类调用基类构造函数,使用“base”关键字从派生类初始化基类。
class A
{
public int a;
public int b;
public A(int x, int y)
{
a = x;
b = y;
}
}
class B : A
{
int c;
public B(int s, int n,int z)
: base(s, n)
{
c = z;
}
public int add()
{
return a + b+c;
}
}
class Program
{
static void Main(string[] args)
{
B b = new B(2, 3,5);
Console.WriteLine(b.add());//the output is 10 OK
}
}
问题
如果派生类从多个基类继承会发生什么情况。那么如何使用 base 关键字从派生类中初始化所有基类(如何调用基类构造函数)。**
class A
{
public int a;
public int b;
public A(int x, int y)
{
a = x;
b = y;
}
}
class B:A
{
public int d;
public int e;
public B(int x, int y)
{
d = x;
e = y;
}
}
class C:B
{
}
然后从 C 类中如何使用 base 关键字初始化两个基类。
【问题讨论】:
-
想象所有人,compiling for themselves。
标签: c# class inheritance