接口的实现与继承

  一个类只能继承一个基类,但是可以继承任意接口;使用多重继承时,要继承的接口之间用逗号隔开。

 

  以下实现的功能为:通过继承接口实现输出进货信息和销售信息的功能。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口的实现与继承
{
    interface Information  //定义接口
    {
        string Code { get; set; }
        string Name { get; set; }
        void ShowInfo();  //用来输出信息
    }

    public class JHInfo : Information   //继承接口,定义进货类
    {
        string code = "";
        string name = "";
        public string Code  //实现编号属性
        {
            get
            {
                return code;
            }
            set
            {
                code = value;
            }
        }

        public string Name
        {
            get  //读
            {
                return name;
            }
            set   //写
            {
                name = value;
            }
        }
        public void ShowInfo()  //输出进货信息
        {
            Console.WriteLine("进货信息:\n"+Code+" "+Name);
        }
    }

    public class XSInfo :Information {//继承接口,实现销售类
        string code = "";
        string name = "";
        public string Code
        {
            get
            {
                return code;
            }
            set
            {
                code = value;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        //实现方法,输出销售信息
        public void ShowInfo() {
            Console.WriteLine("销售信息:\n"+Code+" "+Name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Information[] Infos = {new JHInfo(),new XSInfo()};  //定义接口数组
            Infos[0].Code = "JH0001";  //使用接口对象设置编号属性
            Infos[0].Name = "笔记本电脑";
            Infos[0].ShowInfo();

            Infos[1].Code = "XS0001";
            Infos[1].Name = "小米max3";
            Infos[1].ShowInfo();
            Console.ReadLine();
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值