如何模拟在C#中的多重继承 [英] How to simulate multiple inheritance in C#

查看:155
本文介绍了如何模拟在C#中的多重继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能做到这一点:

  A类:DependencyObject的{} 

B类: DependencyObject的{}

类C:A,b {}


解决方案

C#不具有多重继承,所以行

  C类:A,b {} 

行不通。你可以做类似的事情,虽然接口,沿

 界面了InterfaceA {无效DOA()行; } 
A类:{了InterfaceA公共无效DOA(){}}

接口InterfaceB {无效DOB(); }
B类:InterfaceB {公共无效DOB(){}}

类C:了InterfaceA,InterfaceB
{
M_A =新的A();
M_B =新的B();

公共无效DOA(){m_A.doA(); }
公共无效DOB(){m_B.doB(); }
}


How can I do this:

Class A : DependencyObject {}

Class B : DependencyObject {}

Class C : A , B {}

解决方案

C# does not have multiple inheritance, so the line

Class C : A , B {}

will never work. You can do similar things with interfaces though, along the lines of

interface InterfaceA { void doA(); } 
class A : InterfaceA { public void doA() {} }

interface InterfaceB { void doB(); }
class B : InterfaceB { public void doB() {}}

class C : InterfaceA, InterfaceB
{
  m_A = new A();
  m_B = new B();

  public void doA() { m_A.doA(); }
  public void doB() { m_B.doB(); } 
}

这篇关于如何模拟在C#中的多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆