c# 接口使用實(shí)例
用接口實(shí)現(xiàn)一個(gè)簡(jiǎn)單的物件的入庫(kù),出庫(kù)
如定義一個(gè)物流類接口,包含物件所屬快遞公司名稱屬性,物件單號(hào)屬性及信息顯示方法。通過物件出庫(kù)類信息和物件入庫(kù)類信息繼承該接口。
文檔接口如下:
如下:
(一)接口定義
//定義一個(gè)接口IMyinterface interface IMyinterface { void commodityInformation();//定義一個(gè)快遞信息顯示方法 string Id { get; set; }//定義一個(gè)快遞單號(hào)屬性 string Name { get; set; }///定義一個(gè)快遞所屬快遞公司名稱屬性 }
(二)物件入庫(kù)類
//入庫(kù)類 public class Inbound : IMyinterface { string id = ""; string name = ""; public string Id { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } void IMyinterface.CommodityInformation() { Console.WriteLine("入庫(kù)信息:\n" + "物件單號(hào):" + Id + " " + "所屬快遞公司:" + Name); } }
(三)物件出庫(kù)類
//出庫(kù)類 public class Outbound : IMyinterface { string id = ""; string name = ""; public string Id { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } void IMyinterface.CommodityInformation() { Console.WriteLine("出庫(kù)信息:\n" + "物件單號(hào):" + Id + " " + "所屬快遞公司:" + Name); } }
(四)調(diào)用接口,實(shí)現(xiàn)結(jié)果
1,所先要引用ConsoleApp2如下
2,調(diào)用接口:
static void Main(string[] args) { IMyinterface[] face = { new Inbound(), new Outbound() }; face[0].Id = "X78945612355"; face[0].Name = "申通"; face[0].CommodityInformation(); face[1].Id = "X78912345674"; face[1].Name = "順豐"; face[1].CommodityInformation(); Console.ReadKey(); }
3,實(shí)現(xiàn)結(jié)果如下:
以上就是c# 接口使用實(shí)例的詳細(xì)內(nèi)容,更多關(guān)于c# 接口使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
WinForm實(shí)現(xiàn)移除控件某個(gè)事件的方法
這篇文章主要介紹了WinForm實(shí)現(xiàn)移除控件某個(gè)事件的方法,對(duì)C#初學(xué)者有一定的借鑒價(jià)值,需要的朋友可以參考下2014-08-08C#實(shí)現(xiàn)獲取設(shè)置IP地址小工具
c# 開發(fā),方便更改IP地址。由于公司和家里的ip設(shè)置不一樣,公司要求手動(dòng)設(shè)置,在家可以自動(dòng)獲取IP,切都是無(wú)線網(wǎng)絡(luò),為了方便操作,故做了這個(gè)小工具!2015-06-06c# WPF中System.Windows.Interactivity的使用
這篇文章主要介紹了c# WPF中System.Windows.Interactivity的使用,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03