加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 01:34:42 所属栏目:百科 来源:网络整理
导读:我是一名 Java开发人员,正试图进入C#,我正试图找到一个与Java代码相当的好东西.在Java中,我可以这样做: public interface MyInterface{ public void theMethod();}public abstract class MyAbstractClass implements MyInterface{ /* No interface implemen
我是一名 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. */
    }
}

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

解决方法

你不能,你必须这样做:

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. */ 
    } 
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读