文章出處

回到目錄

之前在我的文章中有對接口進行過講解,但感覺講的還是不夠清晰,不夠利針見血,這次我把面向接口的編程里,自認為比較核心的兩點說一下:

接口詳細介紹請看我的這篇文章

基礎才是重中之重~為什么C#有顯示實現接口

一切依賴于抽象,而不是實現

多個接口相同的行為,被一個對象實現

#region 多個接口相同的行為,被一個對象實現(一切依賴于抽象,而不是實現)
    interface IRepository
    {
        void Insert();
    }

    interface IRepositoryAsync
    {
        void Insert();
    }

    class Repository : IRepository
    {

        #region IRepository 成員

        public void Insert()
        {
            Console.WriteLine("同步添加");
        }

        #endregion
    }
    class RepositoryAsync : IRepository, IRepositoryAsync
    {


        #region ICowBoy 成員

        void IRepository.Insert()
        {
            Console.WriteLine("同步添加");
        }

        #endregion

        #region IRepositoryAsync 成員

        void IRepositoryAsync.Insert()
        {
            Console.WriteLine("異步添加");
        }

        #endregion
    }

    #endregion

接口實現的多態性

一個接口,多種實現(多態)

    #region 一個接口,多種實現(多態)
    interface IHello
    {
        void Morning();
        void Noon();
        void Night();
    }

    class Chinese : IHello
    {

        #region IHello 成員

        public void Morning()
        {
            Console.WriteLine("早上好");
        }

        public void Noon()
        {
            Console.WriteLine("中午好");
        }

        public void Night()
        {
            Console.WriteLine("晚上好");
        }

        #endregion
    }
    class English : IHello
    {
        #region IHello 成員

        public void Morning()
        {
            Console.WriteLine("Good Morning");
        }

        public void Noon()
        {
            Console.WriteLine("Good Noon");
        }

        public void Night()
        {
            Console.WriteLine("Good Night");
        }

        #endregion
    }

    #endregion

對于我們開發人員來說,有時,對一個知識的真正理解是需要一個過程,一個時間的,所以建議初學者,應屆畢業生同學不用太著急,這個是需要一個過程的,呵呵!

回到目錄


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()