我试图使用方法扩展到克服缺乏在C#中的多重继承的支持,并能够继承(几乎继承)几类:C# - 使用方法扩展进行多重继承?
public interface IA
{
// nothing
}
public static class A
{
public static void FunctionA(this IA a)
{
// do something
}
}
public interface IB
{
// nothing
}
public static class B
{
public static void FunctionB(this IB b)
{
// do something
}
}
public class Derived : IA, IB
{
// members
}
// main code
Derived myDerivedobj = new Derived();
myDerivedobj.FunctionA(); // Call Function A inherited from Class A
myDerivedobj.FunctionB(); // Call Function B inherited from Class B
尽管此代码的工作,我能够利用的功能在派生类中由类A和类B公开,这是实现它的正确方法吗?
谢谢
我不知道你的“适当”的意思,但它绝对是一个常用的方法。另外:https://stackoverflow.com/q/2456154/302248 – Sach
如果你的接口实际上没有任何成员,那么这两个函数就可以接受'object'实例,并且根本就不需要接口。 – Servy