如何使用派生类中基类中声明的变量
我有一个在其方法中使用变量的基类,还有一个在其方法中需要相同变量的派生类。 以下是详细信息
abstract class BaseClass
{
protected Transition transition;
public event EventHandler ActionComplete;
private string abc;
Public string ABC
{
get{ return abc;}
set { abc = value;}
}
public void TransitionState(BaseClass obj)
{
ActionComplete(this, null);
}
public abstract void RequestSomeAction(Transition obj);
}
internal class DerivedClass : BaseClass
{
//do i need to create transition variable again here
internal new Transition transition;
//this parameter's value (here obj) should be assigned to the base class,
public override void RequestSomeAction(Transition obj)
{
//is below code correct.
stateTransition = obj;
base.transition= transition;
}
}
I have a base class which uses a variable in ones of its methods and also a derived class that needs the same vaiable in its methods.
Below are the details
abstract class BaseClass
{
protected Transition transition;
public event EventHandler ActionComplete;
private string abc;
Public string ABC
{
get{ return abc;}
set { abc = value;}
}
public void TransitionState(BaseClass obj)
{
ActionComplete(this, null);
}
public abstract void RequestSomeAction(Transition obj);
}
internal class DerivedClass : BaseClass
{
//do i need to create transition variable again here
internal new Transition transition;
//this parameter's value (here obj) should be assigned to the base class,
public override void RequestSomeAction(Transition obj)
{
//is below code correct.
stateTransition = obj;
base.transition= transition;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么要使用新的转换变量和base.transition?如果您在“基类”中保护变量,您可以直接在扩展该类的“子类”中使用它!?
Why the new transition variable and base.transition? If you make a variabele protected in the 'base class' you can just use it directly in the 'child class' that extends that class!?
首先,如果您不需要重新声明变量,就不要重新声明。您可以只使用受保护的属性。
如果您需要具有相同名称的变量的 2 个副本(我想不出您需要这个的任何情况),那么您在技术上是“隐藏”而不是覆盖,所以您可以这样做:
或相同的
MSDN 基础。
First if you don't need to redeclare the variable, don't. You can just use the protected property.
If you need 2 copies of the variable with the same name (i can't think of any case when you would need this) then you are technically 'hiding' versus overridding so you could do:
or the same
MSDN on base.