c#继承关于继承构造函数比如 class B(int x ,string y) :A (x)

来源:百度知道 编辑:UC知道 时间:2024/05/31 07:30:59
B类继承自A类,其中那个A(x)是什么意思,它代表的是什么?
其实,我是想问,那个A(x),其中的x是代表的什么?

这是B类的构造函数声明,不过前面也不应该有class,
冒号后面是这个构造函数的初始化列表,表示将调用A的构造函数函数,传参x.

A(x)代表是的父类中声明的一个变量
像class B(int x ,string y)
也可以是 class A(int x)
不知道你是否明白。。。
感觉自己解释的也很模糊...
你再搜搜,比较基础。

你的父类的构造函数是
public A(int x) 吧。
这样就是你构造类b对像的时候调用他父类的构造函数
抄个例子给你或许清楚点

public class MyBase
{
int num;

public MyBase()
{
Console.WriteLine("in MyBase()");
}

public MyBase(int i )
{
num = i;
Console.WriteLine("in MyBase(int i)");
}

public int GetNum()
{
return num;
}
}

public class MyDerived: MyBase
{
// This constructor will call MyBase.MyBase() <