基礎才是(shi)重(zhong)中(zhong)之重(zhong)~再說面向接口的編程
之前在我(wo)的文章(zhang)中有對接(jie)口(kou)進行過講解,但感(gan)覺講的還是不(bu)夠清晰(xi),不(bu)夠利針見血,這次我(wo)把(ba)面向接(jie)口(kou)的編程里,自認為比(bi)較核心的兩點說一下:
接口詳細介紹請看我的這篇文章
一切依賴于抽象,而不是實現
多個接口(kou)相同的(de)行為,被一個對(dui)象實現
#region 多個接口相同的行為,被一個對象實現(一切依賴于抽象,而不是實現) interface IRepository { void Insert(); } interface IRepositoryAsync { void Insert(); } class Repository : IRepository { #region IRepository 成員 public void Insert() { Console.WriteLine("同步添加(jia)"); } #endregion } class RepositoryAsync : IRepository, IRepositoryAsync { #region ICowBoy 成員 void IRepository.Insert() { Console.WriteLine("同步添加"); } #endregion #region IRepositoryAsync 成員 void IRepositoryAsync.Insert() { Console.WriteLine("異步添加"); } #endregion } #endregion
接口實現的多態性
一個(ge)接口,多種實現(多態)
#region 一個接口,多種實現(多態) interface IHello { void Morning(); void Noon(); void Night(); } class Chinese : IHello { #region IHello 成員 public void Morning() { Console.WriteLine("早上好"); } public void Noon() { Console.WriteLine("中午(wu)好"); } 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
對于我們(men)開發(fa)人員來說,有時,對一(yi)(yi)個知識(shi)的真(zhen)正理解是(shi)(shi)需要一(yi)(yi)個過(guo)程,一(yi)(yi)個時間(jian)的,所以建議初學者,應屆(jie)畢業(ye)生同學不(bu)用太著急,這個是(shi)(shi)需要一(yi)(yi)個過(guo)程的,呵呵!