【发布时间】:2013-03-31 07:19:58
【问题描述】:
public interface IA
{
void DoSomething();
void Calculate();
}
public interface IB
{
void DoSomethingElse();
void Calculate();
}
public class A : IA
{
void DoSomething() { }
void Calculate() {}
}
public class B : IB
{
void DoSomethingElse() { }
void Calculate() {}
}
public class C : IA, IB
{
//How can I implement Calculate() in class B and DoSomething() in class A?
}
如何避免 C 类中的重复代码。参考:How to simulate multiple inheritance in C#。我不想在 C 类中再次编写完整的方法。感谢您的帮助。
【问题讨论】:
-
但您不是“再次编写完整的方法”。您显然正在使用委托并将实现传递给
A和B的嵌入式实例。 -
是的,你是对的。对不起,也许我措辞不正确。在 C 类中,如果可能,我想使用类型转换而不是“public void DoSomething(){m_A.DoSomething();}”
-
@KevenDiuliy Type cast?你想如何使用类型转换?我想你一定是别的意思。
-
我认为在您的示例类
C中,您需要明确实现IA.Calculate()和IB.Calculate()。
标签: c# asp.net multiple-inheritance