C# 顯示實現接口

顯示實現接口的目的就是爲了同名方法。spa

接口是多實現的,好比說一個方法要實現多個接口,而後這幾個接口中有同名方法,這個時候就用到了接口的顯示實現。code

顯示實現接口 成員方法的調用: 接口名.方法名  訪問修飾符private 防止類調用blog

 

1.多個接口 包含同名方法接口

interface Interface1
    {
        void Say();
    }
    interface Interface2
    {
        void Say();
    }

2.經過類實現上述接口it

  public class P1 : Interface1, Interface2
    {
        public void Say()
        {
            Console.WriteLine("hhh");
        }
       //接口顯示實現 默認訪問修飾符爲private 且只能是private
        void Interface2.Say()
        {
            Console.WriteLine("嘿嘿嘿");
        }
    }

3.顯示接口調用  接口名.方法名class

            P1 p1 = new P1();
            p1.Say();//第一個say方法的調用

            Interface2 interface2 = new P1();
            interface2.Say();//第二個say方法的調用
相關文章
相關標籤/搜索