简体   繁体   English

C#覆盖抽象成员

[英]C# override abstract member

Heres my code where im tryng to override SettingsFileName member: 继承我的代码,我试着覆盖SettingsFileName成员:

    public class ProcessorTest: Processor
    {
        public virtual override string SettingsFileName
        {
            get { return @"C:\Settings.xml"; }
        }
    }

Heres the class where is the member: 继承人所在的班级:

    public abstract class Processor
    {
        /// <summary>
        /// Implement this property to enable initializing singleton from the correct file path
        /// </summary>
        public abstract string SettingsFileName { get; }
    }

But this gives me a error: 但这给了我一个错误:

A member 'ProcessorTest.SettingsFileName' marked as override cannot be marked as new or virtual

What am i doing wrong? 我究竟做错了什么?

Just use override in the inherited class, not virtual override . 只需在继承的类中使用override ,而不是virtual override

virtual marks a member as overridable, so it's used in the base class. virtual将成员标记为可覆盖,因此它在基类中使用。 override marks a member as overriding someone overridable. override将成员标记为覆盖某个可覆盖的人。 abstract implies virtual . abstract意味着virtual

remove the virtual here 删除这里的虚拟

public class ProcessorTest: Processor
{
    public override string SettingsFileName
    {
        get { return @"C:\Settings.xml"; }
    }
}

keywords can not be used together 关键字不能一起使用

As it overrides an abstract Property, SettingsFileName cannot be virtual. 由于它覆盖了一个抽象属性,因此SettingsFileName不能是虚拟的。

You cannot use the modifiers new, static, virtual, or abstract to modify an override method. 您不能使用修饰符new,static,virtual或abstract来修改覆盖方法。

See MSDN for more information on override and this article on abstract . 有关override更多信息和有关abstract 文章 ,请参阅MSDN

You need to remove virtual keyword in ProcessorTest . 您需要删除ProcessorTest中的virtual关键字。

override means "override virtual function", so there is no need to write it again. override意味着“覆盖虚函数”,因此无需再次编写它。

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

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