【发布时间】:2019-09-04 09:16:49
【问题描述】:
我有一个父类 Customer,它有 3 个 properties **
CID,Bal,Cname
**
我有一个具有 1 个属性 Stats 的子类。现在我想通过静态 void main 从我的子类构造函数向父类 constructor 提供值。
我想从基类向父类构造函数提供值只要。
我的代码如下
static void Main(string[] args)
{
Stat s1 = new Stat(false);//want to provide value to base class constructor from here only.
}
}
class Customer
{
int _Cid, _Bal;
string _Cname;
public int CID
{
get
{
return _Cid;
}
set
{
_Cid= value;
}
}
public int Bal
{
get
{
return _Bal;
}
set
{
_Bal = value;
}
}
public string Cname
{
get
{
return _Cname;
}
set
{
_Cname = value;
}
}
public Customer(int _Cid,int _Bal,String _Cname)
{
this._Cid=_Cid;
this._Cname = _Cname;
this._Bal = _Bal;
}
}
class Stat:Customer
{
bool _Status;
public bool Stats
{
get
{
return _Status;
}
set
{
_Status= value;
}
}
public void display()
{
}
public Stat(bool _Status):base(int _Cid, int _Bal, String _Cname) //child class constructor how can i supply value to parent class constructor.
{
this._Status = _Status;
}
}
【问题讨论】:
-
您必须定义一个接受正确值的派生类构造函数,然后传递它们。
-
已经有@MarkBenningfield
-
不,你没有。发布的代码甚至无法编译。
-
是的,因为我不知道如何为父类构造函数@MarkBenningfield 提供值
标签: c# inheritance properties