当借口IBase同时被Base类和IA继承时,继承自Base和IA的A类是否继承了IBase?
测试结果:是的
那么它是从Base类的实现继承的还是IA中的实现?
Switch(我是谁?)
Case (Base)a:我是继承自Base的实现
Case (IA)a:我是继承自IA的实现
Case(IBase)a:我是继承自IBase的实现
Case a:是否声明了其中的方法,如果是那么声明的优先,否则就是父类的实现。
这个结果提供了一种可能,即通过父类实现子类的通用功能,然后通过父接口应用到所有子接口中,而且子类中可以对相应的方法重写。
测试代码:
Code
namespace ArchitectureTesting
{
internal class A : Base, IA, IBase
{
// Methods
public A();
void IBase.Write();
}
internal class Base : IBase
{
// Methods
public Base();
public void Write();
}
public interface IA : IBase
{
}
public interface IBase
{
// Methods
void Write();
}
internal class InterfaceTesting
{
// Methods
public InterfaceTesting();
public static void Run();
}
internal class Program
{
// Methods
public Program();
private static void Main(string[] args);
}
}
namespace ArchitectureTesting
{
internal class A : Base, IA, IBase
{
// Methods
public A();
void IBase.Write();
}
internal class Base : IBase
{
// Methods
public Base();
public void Write();
}
public interface IA : IBase
{
}
public interface IBase
{
// Methods
void Write();
}
internal class InterfaceTesting
{
// Methods
public InterfaceTesting();
public static void Run();
}
internal class Program
{
// Methods
public Program();
private static void Main(string[] args);
}
}
IL
.namespace ArchitectureTesting
{
.class private auto ansi beforefieldinit A
extends ArchitectureTesting.Base
implements ArchitectureTesting.IA, ArchitectureTesting.IBase
{
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
}
.method private hidebysig newslot virtual final instance void ArchitectureTesting.IBase.Write() cil managed
{
.override ArchitectureTesting.IBase::Write
}
}
.class private auto ansi beforefieldinit Base
extends [mscorlib]System.Object
implements ArchitectureTesting.IBase
{
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
}
.method public hidebysig newslot virtual final instance void Write() cil managed
{
}
}
.class public interface abstract auto ansi IA
implements ArchitectureTesting.IBase
{
}
.class public interface abstract auto ansi IBase
{
.method public hidebysig newslot abstract virtual instance void Write() cil managed
{
}
}
.class private auto ansi beforefieldinit InterfaceTesting
extends [mscorlib]System.Object
{
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
}
.method public hidebysig static void Run() cil managed
{
}
}
.class private auto ansi beforefieldinit Program
extends [mscorlib]System.Object
{
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
}
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
}
}
}
Collapse Types
.namespace ArchitectureTesting
{
.class private auto ansi beforefieldinit A
extends ArchitectureTesting.Base
implements ArchitectureTesting.IA, ArchitectureTesting.IBase
{
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
}
.method private hidebysig newslot virtual final instance void ArchitectureTesting.IBase.Write() cil managed
{
.override ArchitectureTesting.IBase::Write
}
}
.class private auto ansi beforefieldinit Base
extends [mscorlib]System.Object
implements ArchitectureTesting.IBase
{
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
}
.method public hidebysig newslot virtual final instance void Write() cil managed
{
}
}
.class public interface abstract auto ansi IA
implements ArchitectureTesting.IBase
{
}
.class public interface abstract auto ansi IBase
{
.method public hidebysig newslot abstract virtual instance void Write() cil managed
{
}
}
.class private auto ansi beforefieldinit InterfaceTesting
extends [mscorlib]System.Object
{
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
}
.method public hidebysig static void Run() cil managed
{
}
}
.class private auto ansi beforefieldinit Program
extends [mscorlib]System.Object
{
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
}
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
}
}
}
Collapse Types