1. 程式人生 > >顯式接口實現

顯式接口實現

不同 face 顯式 函數 對象 blog 簽名 string 有時

一個類實現的多個接口裏面有相同函數,而多個接口裏的相同簽名函數確實需要不同的實現,此情況下可以用顯示接口避免。

兩點註意:需要加接口名限定前綴,不需要加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; } }

顯式接口實現