简体   繁体   English

导致“ C#虚拟或抽象成员不能为私有”的非虚拟,非抽象方法

[英]Non-virtual, non-abstract method causing “C# virtual or abstract members cannot be private”

Error 1 'Interface.myDerivedClass.myMethod()': virtual or abstract members cannot be private c:\\users\\igmfun\\documents\\visual studio 2012\\Projects\\Interface\\Interface\\Program.cs 16 错误1'Interface.myDerivedClass.myMethod()':虚拟或抽象成员不能为私有c:\\ users \\ igmfun \\ documents \\ visual studio 2012 \\ Projects \\ Interface \\ Interface \\ Program.cs 16

Making a virtual or abstract member private would make it inaccessible to other classes, including derived classes, making it impossible to override the method, making its virtual, or abstract quality meaningless. 将虚拟或抽象成员设为私有会使其他类(包括派生类)无法访问它,从而无法覆盖该方法,从而使其虚拟或抽象质量变得毫无意义。 I get it. 我知道了。

But wait... The compiler is complaining about the member in the derived class, not the base class... And I never declared that member as virtual or abstract... so did the overriding member (the one in the derived class) inherit the virtual or abstract quality from the virtual or abstract member it is overriding? 但是等等...编译器抱怨的是派生类中的成员,而不是基类...而且我从未将成员声明为虚拟或抽象...上覆的成员(派生类中的成员)也是如此。从被重写的虚拟或抽象成员继承虚拟或抽象质量?

Also, if I change 另外,如果我改变

override void myMethod(){}

to

public override void myMethod(){}

I get a new error, 我收到一个新错误,

"No suitable method found to override." “找不到合适的方法可以覆盖。”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Interface
{
    interface myInterface
    {
        void myMethod();
    }

    class myDerivedClass : myInterface
    {
        override void myMethod()
        {
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

        }
    }
}

The error is somewhat misleading - it looks like compiler tried first to reason locally about code. 该错误有些令人误解-看起来编译器首先尝试在本地对代码进行推理。 So it found you are doing override with private (default) specifier. 因此,它发现您正在使用private (默认)说明符进行override This is clearly an error, but it actually hides the real one - there is nothing to override to start with (you can see that if you change code to public override void myMethod(){} ). 显然这是一个错误,但实际上它隐藏了真正的错误-开头没有任何要覆盖的内容(您可以看到,如果将代码更改为public override void myMethod(){} )。

Actual fix is to make method public as it implements an interface method: 实际的解决方法是在实现接口方法时将方法public

class MyInterfaceImplementationClass : IMyInterface
{
    public void MyMethod()
    {
    }
}

Alternatively you can explicitly implement the interface too if you prefer method to be not visible from the class directly (similar to private , but you can call by casting to interface): 另外,如果您希望直接从类中不可见的方法,您也可以显式实现该接口(类似于private ,但是可以通过强制转换为接口来调用):

class MyExplicitInterfaceImplementationClass : IMyInterface
{
    void IMyInterface.MyMethod()
    {
    }
}

interface IMyInterface
{
    void MyMethod();
}

I think you are confusing Interface with Class . 我认为您将InterfaceClass混淆了。

If you want a base class to derive from use the class keyword not interface 如果要基类派生,请使用class关键字not interface

public class BaseClass
{
   public void MyMethod()
   {

   }
}

public class DerivedClass : BaseClass
{

}

If you want to be able to override the method in the base class you can mark it as virtual and use the override keyword in the derived class 如果您希望能够override基类中的方法,则可以将其标记为virtual并在派生类中使用override关键字。

public class BaseClass
{
   public virtual void MyMethod()
   {

   }
}

public class DerivedClass : BaseClass
{
   public override void MyMethod()
   {
      // do soothing different
      base.MyMethod()
   }
}

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

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