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

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

问题描述

我想在 c# 中创建一个抽象类,它从不同的接口继承",但将具体实现留给子类.然而,编译器抱怨说,该类没有实现接口中指定的方法.我已经习惯了 Java,它总是可以工作,所以我不确定它应该如何在 c# 中工作.无论如何,这是我的代码:

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; 
   }
 }

推荐答案

添加抽象方法:

    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天全站免登陆