这篇文章主要介绍了c#接口使用的实例,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
用接口实现一个简单的物件的入库,出库
如定义一个物流类接口,包含物件所属快递公司名称属性,物件单号属性及信息显示方法。通过物件出库类信息和物件入库类信息继承该接口。
文档接口如下:
//定义一个接口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);
}
}
(四)调用接口,实现结果
1,所先要引用ConsoleApp2如下
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# 接口使用的资料请关注得得之家其它相关文章!
沃梦达教程
本文标题为:c# 接口使用实例
猜你喜欢
- Unity Shader实现模糊效果 2023-04-27
- user32.dll 函数说明小结 2022-12-26
- c# 模拟线性回归的示例 2023-03-14
- 在C# 8中如何使用默认接口方法详解 2023-03-29
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- .NET CORE DI 依赖注入 2023-09-27
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- Unity3D实现渐变颜色效果 2023-01-16
- Oracle中for循环的使用方法 2023-07-04
- 如何使用C# 捕获进程输出 2023-03-10