顯式實現接口

注意:

1. 不能提供訪問控制符;

2. 顯示實現的成員總是自動爲私有的

示例:

using System;

namespace ExplicitImpleInterfaceTest
{
    public interface IDrawToMemory
    {
        void Draw();
    }

    public class Triangle : IDrawToMemory
    {
        void IDrawToMemory.Draw()	//此處不能加訪問控制符
        {
            Console.WriteLine("Triangle.Draw...");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Triangle triangle = new Triangle();
            //triangle.Draw();      //錯誤
            if (triangle is IDrawToMemory)
            {
                ((IDrawToMemory)triangle).Draw();
            }
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章