首页 > 编程技术 > csharp

c# 接口使用实例

发布时间:2020-7-17 22:44

用接口实现一个简单的物件的入库,出库

如定义一个物流类接口,包含物件所属快递公司名称属性,物件单号属性及信息显示方法。通过物件出库类信息和物件入库类信息继承该接口。

文档接口如下:

如下:

(一)接口定义

//定义一个接口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如下

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,实现结果如下:

以上就是c# 接口使用实例的详细内容,更多关于c# 接口使用的资料请关注猪先飞其它相关文章!

标签:[!--infotagslink--]

您可能感兴趣的文章: