我可以在C#中的抽象类中省略接口方法吗?

izb*_*izb 3 c# java abstract-class class

我是一名Java开发人员,正试图进入C#,我正试图找到一个与Java代码相当的好东西.在Java中,我可以这样做:

public interface MyInterface
{
    public void theMethod();
}

public abstract class MyAbstractClass implements MyInterface
{
    /* No interface implementation, because it's abstract */
}

public class MyClass extends MyAbstractClass
{
    public void theMethod()
    {
        /* Implement missing interface methods in this class. */
    }
}
Run Code Online (Sandbox Code Playgroud)

什么是C#等同于此?使用abstract/new/override等的最佳解决方案似乎都导致'theMethod'在抽象类中使用某种形式的主体声明.如何在不属于它的抽象类中删除对此方法的引用,同时在具体类中强制执行它?

Kla*_*sen 5

你不能,你必须这样做:

public interface MyInterface 
{ 
    void theMethod(); 
} 

public abstract class MyAbstractClass : MyInterface 
{ 
     public abstract void theMethod();
} 

public class MyClass : MyAbstractClass 
{ 
    public override void theMethod() 
    { 
        /* Implement missing interface methods in this class. */ 
    } 
} 
Run Code Online (Sandbox Code Playgroud)