Welcome

首页 / 软件开发 / C# / 如何在C#中实现多继承

如何在C#中实现多继承2011-07-02 博客园 imbobC # 如何实现多继承,关键在于接口可以写方法的实现

namespace Extensions
{
using System;
using ExtensionMethodsDemo1;


public static class Extension
{
//扩展接口
public static void MethodB(this IMyInterface myInterface)
{
Console.WriteLine("Extension.MethodB(this IMyInterface myInterface)");
}
}
}
namespace ExtensionMethodsDemo1
{
using System;
using Extensions;

public interface IMyInterface
{
void MethodB();
}

class A : IMyInterface
{
public void MethodB(){Console.WriteLine("A.MethodB()");}
}

class B : IMyInterface
{
public void MethodB() { Console.WriteLine("B.MethodB()"); }

}

class C : IMyInterface
{
public void MethodB() { Console.WriteLine("C.MethodB()"); }

}

class ExtMethodDemo
{
static void Main(string[] args)
{
A a = new A();
B b = new B();
C c = new C();

a.MethodB()
b.MethodB()
c.MethodB()

}
}
}