一个类只能继承一个基类,但是可以继承任意接口;使用多重继承时,要继承的接口之间用逗号隔开。
以下实现的功能为:通过继承接口实现输出进货信息和销售信息的功能。
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();
}
}
}