Creating an abstract class that implements multiple interfaces in c#

荒凉一梦 提交于 2020-01-02 03:37:09

问题


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

回答1:


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



回答2:


This is the right way to do it.

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

        public abstract void Dispose();
    }

I dont know the definition of your IPartImportsSatisfiedNotification interface so i my sample can only provide the methods defined in IDisposable... Do it for IPartImportsSatisfiedNotification the same way.




回答3:


You will need to add abstract methods that "implement" those interfaces.

So for instance:

 public abstract void Dispose(); // implements IDisposable



回答4:


You can just declare the methods and properties the interfaces expect as abstract in your abstract class. This forces the subclasses to still do the implementation but doesn't violate C#'s rules of interfaces.




回答5:


abstract class in basics its a normal class so he also has to implements these methods.

if you want further implementations , put the virtual methods ( or abstract) in the abstract class itself




回答6:


As noted by others, you would need to mark the methods as abstract in your base class, which will force derived classes to implement. You can run this as a C# program in LinqPad

void Main()
{   
DerivedClass dc = new DerivedClass("hello, world");
Console.Out.WriteLine(dc);  
string result = dc.Notify("greetings");
Console.Out.WriteLine(result);
}



public interface IPartImportsSatisfiedNotification
{
string Notify(string msg);
}



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

   abstract public string Notify(string msg);

 }

 public class DerivedClass : MyClass
 {
public DerivedClass(string name) :base(name)
{

}

public override string Notify(string msg)
{
    return string.Format("Msg {0} from {1}", msg, this.name);
}

public override string ToString() 
{
    return this.name;
}
 }



回答7:


you need to add abstract method in your abstract class.

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

     }


来源:https://stackoverflow.com/questions/7862439/creating-an-abstract-class-that-implements-multiple-interfaces-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!