如何使用派生类中基类中声明的变量

发布于 2024-11-14 14:36:50 字数 825 浏览 1 评论 0原文

我有一个在其方法中使用变量的基类,还有一个在其方法中需要相同变量的派生类。 以下是详细信息

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南笙 2024-11-21 14:36:50

为什么要使用新的转换变量和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!?

芯好空 2024-11-21 14:36:50

首先,如果您不需要重新声明变量,就不要重新声明。您可以只使用受保护的属性。

this.transition

如果您需要具有相同名称的变量的 2 个副本(我想不出您需要这个的任何情况),那么您在技术上是“隐藏”而不是覆盖,所以您可以这样做:

base.transition

或相同的

((BaseClass)this).transition

MSDN 基础

First if you don't need to redeclare the variable, don't. You can just use the protected property.

this.transition

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:

base.transition

or the same

((BaseClass)this).transition

MSDN on base.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文