简体   繁体   English

无法密封,因为它不是覆盖

[英]Cannot be sealed because it's not an override

I've the following class: 我有以下课程:

namespace Warnings
{
    public abstract class BaseWarningIntField : IWarningInnerDataField
    {
        public string PropName;

        public string HeaderCaption;

        public sealed WarningInnerDataType DataType
        {
            get { return WarningInnerDataType.Integer; }
        }
    }
}

I want the last property DataType to be not overridable, since that's the base class for a warning-detail field of type Integer, so it needs to always return the correct type WarningInnerDataType.Integer . 我希望最后一个属性DataType不可覆盖,因为它是Integer类型的warning-detail字段的基类,所以它需要始终返回正确的类型WarningInnerDataType.Integer

Anyway, the compiler give me the following error: 无论如何,编译器给我以下错误:

'Warnings.BaseWarningIntField.DataType' cannot be sealed because it is not an override 'Warnings.BaseWarningIntField.DataType'无法密封,因为它不是覆盖

But, as far as I know the override does exactly the opposite of what I'm trying to achieve. 但是,据我所知, override与我想要实现的完全相反。

in C# all methods by default are non-virtual. C# ,默认情况下所有方法都是非虚拟的。 You can't override non-virtual method in sub-classes. 您不能覆盖子类中的非虚方法。 So leaving property as usual will safe you from subclass overriding it. 因此,像往常一样离开财产将使您免受子类覆盖它的影响。

Sealed is a keyword used in class declaration for inheritance restrictions or is used to stop virtual chain of members of a class hierarchy. Sealed是继承限制的类声明中使用的关键字,或用于停止类层次结构的成员的虚拟链。 But again - this relates to virtual methods and properties. 但同样 - 这与虚拟方法和属性有关。

Trying to override "normal" property in sub-class will result in compile error 试图覆盖子类中的“普通”属性将导致编译错误

'WarningIntField.DataType.get': cannot override inherited member 'BaseWarningIntField.DataType.get' because it is not marked virtual, abstract, or override 'WarningIntField.DataType.get':无法覆盖继承的成员'BaseWarningIntField.DataType.get',因为它未标记为虚拟,抽象或覆盖

To answer you comment, I'll present some code examples to illustrate my point. 为了回答你的评论,我将提供一些代码示例来说明我的观点。 You can't actually restrict derived classes from hiding a method or property. 实际上,您无法限制派生类隐藏方法或属性。 So next situation is legal and there is no way to overcome it (this related to virtual method and methods denoted with new keyword as well) 所以下一个情况是合法的,没有办法克服它(这与用new关键字表示的虚方法和方法有关)

class BaseClass
{
    public string Property {get; set;}
}

class DerivedClass : BaseClass
{
    //compiler will give you a hint here, that you are hiding a base class prop
    public string Property {get; set;}
}

The same way you can't restrict of hiding a field in a class by local variable, so this situation is also valid. 同样地,您不能限制通过局部变量隐藏类中的字段,因此这种情况也是有效的。 Note that compiler will also help you to note, that you are hiding class field in by a local variable. 请注意,编译器还可以帮助您注意,您正在通过局部变量隐藏类字段。 This also related to readonly const and simple static fields as well. 这也与readonly const和简单的static字段有关。

int field = 0; //class field
void Foo()
{
    int field = 0; //local variable
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM