用接口实现一个简单的物件的入库,出库
如定义一个物流类接口,包含物件所属快递公司名称属性,物件单号属性及信息显示方法。通过物件出库类信息和物件入库类信息继承该接口。
文档接口如下:
如下:
(一)接口定义
1
2
3
4
5
6
7
|
//定义一个接口IMyinterface interface IMyinterface { void commodityInformation(); //定义一个快递信息显示方法 string Id { get ; set ; } //定义一个快递单号属性 string Name { get ; set ; } ///定义一个快递所属快递公司名称属性 } |
(二)物件入库类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//入库类 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); } } |
(三)物件出库类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//出库类 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); } } |
(四)调用接口,实现结果
1,所先要引用ConsoleApp2如下
2,调用接口:
1
2
3
4
5
6
7
8
9
10
11
12
|
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,实现结果如下:
以上就是c# 接口使用实例的详细内容,更多关于c# 接口使用的资料请关注服务器之家其它相关文章!
原文链接:https://www.cnblogs.com/m962263807/p/13301109.html