[英]how to access the value of the member variables of a base class from a derived class
i have 3 classes: class PassengerDetails which is inherited by class FlightDetails which in turn is inherited by class Details. 我有3个类:FlightDetails类继承的PassengerDetails类,而Details类又继承了FlightDetails类。 Both, PassengerDetails and FlightDetails have a method Accept which accepts some parameters and assigns it to local variables (declared as protected) of that class.
PassengerDetails和FlightDetails都具有方法Accept,该方法接受一些参数并将其分配给该类的局部变量(声明为protected)。 What i need to do is by using a method Show in Details i want to print the values of those local variables of class PassengerDetails and FlightDetails.
我需要做的是使用“在详细信息中显示”方法,我要打印“旅客详细信息”和“航班详细信息”类的那些局部变量的值。 here are few parts of my entire code:
这是我整个代码的一部分:
class PassengerDetails
{
protected string strFirstName;
protected int iAge;
public void Accept(string FirstName, int Age)
{
strFirstName = FirstName;
iAge = Age;
}
}
class FlightDetails:PassengerDetails
{
protected DateTime dt;
protected int iNumPass;
public void Accept_1(DateTime date,int NumPass)
{
dt = date;
iNumPass = NumPass;
}
}
class Details : FlightDetails
{
public void Show(Label lbl)
{
lbl.Text= "" + strFirstName +"\n"+ iAge +"\n"+ dt.ToString() + "\n"+ iNumPass;
}
private void btnSubmit_Click(object sender, EventArgs e)
{
PassengerDetails pass = new PassengerDetails();
pass.Accept(txtFName.Text,Convert.ToInt32(txtAge.Text));
}
private void btn2Submit_Click(object sender, EventArgs e)
{
FlightDetails fgt = new FlightDetails();
fgt.Accept_1(Convert.ToDateTime(dtDep.Text),Convert.ToInt32(txtNPass.Text));
Details det = new Details();
det.Show(lblShow);
when i do this, all i get is default value of those local variables. 当我这样做时,我得到的只是那些局部变量的默认值。 can someone plzz help???
有人可以帮助吗???
You are calling th Accept
and Accept_1
methods on separate instances of the classes in question. 您正在有关的类的单独实例上调用
Accept
和Accept_1
方法。 Your Details
class exposes these directly by inheritance, so you should replace this: 您的
Details
类直接通过继承公开这些内容,因此应替换为:
PassengerDetails pass = new PassengerDetails();
pass.Accept(txtFName.Text,Convert.ToInt32(txtAge.Text));
with this: 有了这个:
this.Accept(txtFName.Text,Convert.ToInt32(txtAge.Text));
or, more simply (arguably): 或者,更简单地说(可以说):
Accept(txtFName.Text,Convert.ToInt32(txtAge.Text));
The point is, your instance of Details
is already also an instance of FlightDetails
and PassengerDetails
, so you don't need to create separate ones. 关键是,您的
Details
实例已经是FlightDetails
和PassengerDetails
的实例,因此您无需创建单独的实例。 However, looking at the class names, I strongly suspect that you might be misusing inheritance, and that a composition approach may be more appropriate, with Details
exposing properties of types FlightDetails
and PassengerDetails
. 但是,查看类名称时,我强烈怀疑您可能会滥用继承,并且组合方法可能更合适,并且
Details
公开FlightDetails
和PassengerDetails
类型的属性。
You can access to values of FlightDetails by 您可以通过以下方式访问FlightDetails的值
base.fieldname
and for access to values of PassengerDetails, add a property to FlightDetails : 为了访问PassengerDetails的值,请在FlightDetails中添加一个属性:
public type property
{
get {return base.fieldname;}
}
and in Details use property. 并在“详细信息”使用属性中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.