How to simulate multiple inheritance in C#

筅森魡賤 提交于 2019-11-27 06:02:29

问题


How can I do this:

Class A : DependencyObject {}

Class B : DependencyObject {}

Class C : A , B {}

回答1:


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(); } 
}



回答2:


You can't do multiple inheritance in C# but you can implement multiple interfaces:

  1. Create an interface with the same methods as the public members of A
  2. Create an interface with the same methods as the public members of B
  3. Create member variables for A and B inside of C.
  4. Implement the A interface from both A and C (C's implementation simply calls its member variable of type A)
  5. Implement the B interface from both B and C (C's implementation simply calls its member variable of type B)



回答3:


You can't, C# does not support multiple inheritance of classes.

If you provide a more concrete example of what you're trying to do, maybe we can give you a better solution (for example, perhaps composition is what you're after, rather than inheritance - of course, I can't tell from this simple example).




回答4:


For better or worse C# does not support multiple inheritance. However, I have had limited success in simulating it by using extension methods. The extension method approach might look like this.

public class A
{
  public void DoSomething() { }
}

public static class B
{
  public static void DoSomethingElse(this A target) { }
}

public class C : A
{
}

The obvious problem here is that C is definitely not a B. So the only thing this is good for is avoiding duplicate code in some situations.

On the other hand C# does support implementing multiple interfaces. It would look like this.

public interface IA
{
  void DoSomething();
}

public interface IB
{
  void DoSomethingElse();
}

public class A : IA
{
  void DoSomething() { }
}

public class B : IB
{
  void DoSomethingElse() { }
}

public class C : IA, IB
{
  private A m_A = new A();
  private B m_B = new B();

  public void DoSomething() { m_A.DoSomething(); }

  public void DoSomethingElse() { m_B.DoSomethingElse(); }
}

The obvious problem here is that while you are inheriting the interface you are not inheriting the implementation. This is good for polymorphism, but bad for avoiding duplicate code.

Sometimes you can use both strategies together to cludge together something resembling multiple inheritance, but it is probably going to be difficult to understand and maintain. If your situation really does call for multiple inheritance then your options are going to limited and certainly not ideal, but they do exist nevertheless.




回答5:


You cannot have C be both an A and a B unless one of those inherits from the other.

However, if you want C to have behaviours from A and B that are not common to both of them, use an interface.




回答6:


Another way, which does not use composition, uses extension methods. Define interfaces with no methods, and implement them with your C class. Then, write a static class with extension methods that provide implementations against your interface.

public static void MyMethod(this InterfaceA a)
{
    // do stuff with a
}

downside of this is that you can only work with those properties of the object that are defined in the interface. This limits you to auto-implemented properties and method calls, basically.




回答7:


You can achieve this by Multilevel Inheritance...

 public class DependencyObject
{
    public void DependencyObjectMethod() { }
}

public class A : DependencyObject
{
    public void MethodA() { }
}

public class B : A
{
    public void MethodB() { }
}

public class C : B
{
    public void MethodC()
    {
        //Do Something
    }
}

By this you can get access to all the methods and properties of those classes.




回答8:


So I guess it cant be done so ...

class A : DependencyObject
    {
        public int X
        {
            get { return (int)GetValue(XProperty); }
            set { SetValue(XProperty, value); }
        }
        public static readonly DependencyProperty XProperty = DependencyProperty.Register("X", typeof(int), typeof(A), new UIPropertyMetadata(0));
    }

    class B : DependencyObject
    {
        public int X
        {
            get { return (int)GetValue(XProperty); }
            set { SetValue(XProperty, value); }
        }
        public static readonly DependencyProperty XProperty = DependencyProperty.Register("X", typeof(int), typeof(B), new UIPropertyMetadata(0));
    }

    class C : DependencyObject
    {
        A a = new A();
        B b = new B();
        public int X
        {
            get { return a.X; }
            set { a.X = value; }
        }
        public int Y
        {
            get { return b.X; }
            set { b.X = value; }
        }
    }


来源:https://stackoverflow.com/questions/3199956/how-to-simulate-multiple-inheritance-in-c-sharp

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