在类A中实现了某些接口,以后在继承类A时也会一起继承所有接口的实现,而不需要在继承类中显式地进行接口的实现。除非在继承类中想要改变父类的接口实现。具体的代码如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
interface ITest
{
void TestMethod1();
}
public class TestClass1 : ITest
{
public void TestMethod1()
{
Console.WriteLine("TestClass1 TestMethod1");
}
}
public class TestInheritClass1 : TestClass1
{
}
public class TestInheritClass2 : TestClass1, ITest
{
public void TestMethod1()
{
Console.WriteLine("TestInheritClass2 TestMethod1");
}
}
class Program
{
static void Main(string[] args)
{
ITest testInterface = new TestClass1();
ITest testInheritInterface1 = new TestInheritClass1();
ITest testInheritInterface2 = new TestInheritClass2();
testInterface.TestMethod1();
testInheritInterface1.TestMethod1();
testInheritInterface2.TestMethod1();
Console.ReadLine();
}
}
}
程序执行的结果如下图所示: