问题
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