您的位置:首页 > 其它

实现接口的类是否继承接口实现的问题

2008-07-13 11:21 537 查看
在类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();

        }

    }

}

程序执行的结果如下图所示:

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  class interface string
相关文章推荐