c# 接口使用實例
更新時間:2020年07月17日 16:06:49 作者:莫得感情的代碼機器
這篇文章主要介紹了c#接口使用的實例,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
用接口實現(xiàn)一個簡單的物件的入庫,出庫
如定義一個物流類接口,包含物件所屬快遞公司名稱屬性,物件單號屬性及信息顯示方法。通過物件出庫類信息和物件入庫類信息繼承該接口。
文檔接口如下:
如下:
(一)接口定義
//定義一個接口IMyinterface interface IMyinterface { void commodityInformation();//定義一個快遞信息顯示方法 string Id { get; set; }//定義一個快遞單號屬性 string Name { get; set; }///定義一個快遞所屬快遞公司名稱屬性 }
(二)物件入庫類
//入庫類 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("入庫信息:\n" + "物件單號:" + Id + " " + "所屬快遞公司:" + Name); } }
(三)物件出庫類
//出庫類 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("出庫信息:\n" + "物件單號:" + Id + " " + "所屬快遞公司:" + Name); } }
(四)調用接口,實現(xiàn)結果
1,所先要引用ConsoleApp2如下
2,調用接口:
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,實現(xiàn)結果如下:
以上就是c# 接口使用實例的詳細內容,更多關于c# 接口使用的資料請關注腳本之家其它相關文章!
相關文章
c# WPF中System.Windows.Interactivity的使用
這篇文章主要介紹了c# WPF中System.Windows.Interactivity的使用,幫助大家更好的理解和學習使用c#,感興趣的朋友可以了解下2021-03-03