创建,在C#实现多个接口一个抽象类 [英] Creating an abstract class that implements multiple interfaces in c#

查看:388
本文介绍了创建,在C#实现多个接口一个抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想C#创建一个抽象类,从不同的界面,继承,但留下的具体落实到子类。然而,编译器抱怨,该类犯规实现接口指定的方法。我已经习惯了Java的这哪里总是工作,所以我不知道它是如何应该在C#中工作。无论如何,这是我的代码:

 公共抽象类MyClass的:IDisposable接口,IPartImportsSatisfiedNotification 
{
私人字符串名称;
公共MyClass的(字符串名称)
{
this.name =名称;
}
}


解决方案

添加抽象方法:

 公共接口IPartImportsSatisfiedNotification 
{
无效的someMethod();
}

公共抽象类MyClass的:IDisposable接口,IPartImportsSatisfiedNotification
{
私人字符串名称;
公共MyClass的(字符串名称)
{
this.name =名称;
}

公共抽象无效的someMethod();

公共抽象无效的Dispose();
}

公共类亚纲:MyClass的
{
公共SubClass中(字符串someString):基地(someString)
{

}

公共覆盖无效的someMethod()
{
抛出新NotImplementedException();
}

公众覆盖无效的Dispose()
{
抛出新NotImplementedException();
}
}


I'd like to create an abstract class in c#, that "inherits" from different interfaces, but leaves the concrete implementation to the subclass. The compiler however complains, that the class doesnt implement the methods specified in the interfaces. I'm used to Java where this always worked, so I'm not sure how it is supposed to work in c#. Anyway, this is my code:

 public abstract class MyClass : IDisposable, IPartImportsSatisfiedNotification
 {
   private string name; 
   public MyClass(string name)
   {
       this.name = name; 
   }
 }

解决方案

Add abstract methods:

    public interface IPartImportsSatisfiedNotification
    {
        void SomeMethod();
    }

    public abstract class MyClass : IDisposable, IPartImportsSatisfiedNotification
    {
        private string name;
        public MyClass(string name)
        {
            this.name = name;
        }

        public abstract void SomeMethod();

        public abstract void Dispose();
    }

    public class SubClass : MyClass
    {
        public SubClass(string someString) : base(someString)
        {

        }

        public override void SomeMethod()
        {
            throw new NotImplementedException();
        }

        public override void Dispose()
        {
            throw new NotImplementedException();
        }
    }

这篇关于创建,在C#实现多个接口一个抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆