码迷,mamicode.com
首页 > 其他好文 > 详细

显式接口实现

时间:2017-06-04 20:08:33      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:int   实现   pre   private   注意   log   line   back   ons   

一个类实现的多个接口里面有相同函数,而多个接口里的相同签名函数确实需要不同的实现,此情况下可以用显示接口避免。

两点注意:需要加接口名限定前缀,不需要加public修饰符,因为显式接口成员只能通过接口来使用,不能通过对象引用使用,所以有时是public,有时是private,不需要加public修饰符。

    interface IInterface
    {
        int Test(string name);
    }

    interface IInterface2
    {
        int Test(string name);
    }

    class MyClass : IInterface, IInterface2
    {
        int IInterface.Test(string name)
        {
            Console.WriteLine("int IInterface.Test(string name)");

            return 1;
        }

        int IInterface2.Test(string name)
        {
            Console.WriteLine("int IInterface2.Test(string name)");

            return 1;
        }
    }

 

显式接口实现

标签:int   实现   pre   private   注意   log   line   back   ons   

原文地址:http://www.cnblogs.com/dirichlet/p/3327143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!