• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

c# 接口使用实例

c# 搞代码 4年前 (2022-01-09) 15次浏览 已收录 0个评论

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

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

文档接口如下:

如下:

(一)接口定义

//定义一个接口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 = "申<i style="color:transparent">本文来源gaodai$ma#com搞$$代**码)网8</i>通";
      face[0].CommodityInformation();

      face[1].Id = "X78912345674";
      face[1].Name = "顺丰";
      face[1].CommodityInformation();
      Console.ReadKey();
    }

3,实现结果如下:

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


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:c# 接口使用实例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址