基类方法使用的 C# 成员变量覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1112458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:31:45  来源:igfitidea点击:

C# Member variable overrides used by base class method

c#overriding

提问by

OK so I admit right off the top that this is a bit screwy … but it does serve a logical purpose. I'm using C# for a current project and I'm trying to find a way to override a member variable in a derived class, but access the overridden variable in a base class method. To make things more “entertaining” it would be preferable if the overridden member variable was static (this is NOT shown in the example code below).

好的,所以我直接承认这有点棘手……但它确实符合逻辑目的。我在当前项目中使用 C#,我试图找到一种方法来覆盖派生类中的成员变量,但在基类方法中访问被覆盖的变量。为了使事情更“有趣”,如果被覆盖的成员变量是静态的(这在下面的示例代码中没有显示)会更好。

Here is my sample code:

这是我的示例代码:

class baseclass
{
    protected string[] array = null;

    public string method()
    {
        string str = "";
        foreach (string x in this.array)
        {
            str += x + "  "; 
        }

        return str;
    }
}

class subclass1 : baseclass
{
    new string[] array = new string[]
    {
        "class1value1",
        "class1value2",
        "class1value3",
        "class1value4"
    };
}

class subclass2 : baseclass
{
    new string[] array = new string[]
    {
        "class2value1",
        "class2value2",
        "class2value3",
        "class2value4"
    };
}

Any thoughts as to why this doesn't work and a way to get around it?

关于为什么这不起作用以及解决方法的任何想法?

采纳答案by JaredPar

Is there any reason you can't use a virtual property? That would provide exactly the functionality you are looking for. It just wouldn't be a field.

有什么理由不能使用虚拟财产吗?这将提供您正在寻找的功能。它不会是一个领域。

protected abstract string[] array { get; }

...

...

protected override string[] array { get { return new string[]{"...","..."}; }}

回答by Adrian Godong

Why do you need to override the variable? Looking from your code, just setting the values would be enough, no?

为什么需要覆盖变量?从您的代码来看,只需设置值就足够了,不是吗?

Plus, static variables are tied to the class (not the instance), therefore it's not overridable on any situation.

另外,静态变量与类(而不是实例)相关联,因此在任何情况下都不可覆盖。

回答by McAden

Simply don't use new. Set the array in your subclass' constructor.

只是不要使用新的。在子类的构造函数中设置数组。

EDIT: with code:

编辑:使用代码:

class subclass1 : baseclass
{
    public subclass1()
    {
        array = new string[]
        {
            "class1value1",
            "class1value2",
            "class1value3",
            "class1value4"
        };
    }
}

class subclass2 : baseclass
{
    public subclass2()
    {
        array = new string[]
        {
            "class2value1",
            "class2value2",
            "class2value3",
            "class2value4"
        };
    }
}

回答by dtb

class BaseClass
{
    public virtual string Method()
    {
        return string.Empty;
    }
}

abstract class BaseClass<T> : BaseClass where T : BaseClass<T>
{
    protected static string[] strings;

    public override string Method()
    {
        return string.Join("  ", strings);
    }
}

class Subclass1 : BaseClass<Subclass1>
{
    static Subclass1()
    {
        strings = new[] { "class1value1", "class1value2", "class1value3" };
    }
}

class Subclass2 : BaseClass<Subclass2>
{
    static Subclass2()
    {
        strings = new[] { "class2value1", "class2value2", "class2value3" };
    }
}

The important part is the generic parameter Twhich basically functions as an index to the string arrays.

重要的部分是泛型参数T,它基本上用作字符串数组的索引。

回答by Archangel

You're trying to get polymorphic behavior from the array but you're defining it for class local behavior.

您试图从数组中获取多态行为,但您正在为类本地行为定义它。

The array has to be virtual or method() in the base class will compile to always access the array in the base class - the array should also be a property not a field to do this, i.e.,

数组必须是虚拟的,否则基类中的 method() 将编译为始终访问基类中的数组 - 数组也应该是一个属性而不是一个字段来执行此操作,即,

string[] _array = { ... }; /base class local values protected virtual string[] array { get { return _array; } } //makes the array property overridable

字符串[] _array = { ... }; /基类本地值受保护的虚拟字符串[]数组{获取{返回_数组;} } //使数组属性可覆盖

then in the subclasses, you'll need to do

然后在子类中,你需要做

string[] _array = { ... }; //subclass local values virtual string[] array { get { return _array; } } //this overrides the base class property

字符串[] _array = { ... }; //子类局部值 virtual string[] array { get { return _array; } } //这会覆盖基类属性

in order to get the desired affect

为了得到想要的效果