How to derive two abstract classes in C#

我怕爱的太早我们不能终老 提交于 2019-12-11 03:29:13

问题


I have app structure -

public abstract class a 
{
}
//defined in a.dll
public abstract class b
{
}
//defined in b.dll

//Above 2 DLL reference added in main project where I want to derive both of this abstract classee like

public abstract class proj : a, b
{
}

I am able to derive any one of it only not both. So pls guide me on missing things or wrong coding I had done.


回答1:


You can't multiple inherit using C#. You can however achieve this by using an interface.

public interface Ia 
{

}

public interface Ib 
{

}

public abstract class MainProject : Ia, Ib  
{

}

C# interfaces only allow signatures of methods, properties, events and indexers. You will have to define the implementation of these in the proj (MainProgram) class.




回答2:


public abstract class proj : a, b

This can't be done. C# doesn't allow multiple inheritance.




回答3:


You can't derive from two classes at the same time. You should use interfaces instead.

public interface IFirstInterface
{
}
public interface ISecondInterface
{
}

public abstract class Proj : IFirstInterface, ISecondInterface
{
}

Now classes that inherit from Proj will still need to implement all methods and properties defined in both interfaces.




回答4:


Rather than deriving from multiple abstract classes (which is illegal in C#), derive from two interfaces, (which are abstract by definition).



来源:https://stackoverflow.com/questions/15432431/how-to-derive-two-abstract-classes-in-c-sharp

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