using System;
namespace Duck
{
interface IFly//interface接口
{
void Fly();
//void FlyTwo();
}
interface IFlyTwo:IFly//继承接口
{
void FlyTwo();
//void FlyTwo();
}
interface IFlyThree : IFlyTwo,IFly//继承多个接口
{
//void FlyTwo();
}
abstract class DK
{
/// <summary>
///抽象类里面只能是方法、属性、字段
/// </summary>
public string id;
public string color;
public float weight;
public abstract void Quack();
public abstract void Swimming();
}
class Duck : DK
{
public override void Quack()
{
Console.WriteLine("嘎嘎嘎");
}
public override void Swimming()
{
Console.WriteLine("游啊游");
}
public Duck(string _id, string _color, float _weight)
{
this.id = _id;
this.color = _color;
this.weight = _weight;
}
}
class FlyDuck : DK, IFlyThree//必须实现?
{
public override void Quack()
{
Console.WriteLine("嘎嘎嘎");
}
public override void Swimming()
{
Console.WriteLine("游啊游");
}
public void Fly()
{
Console.WriteLine("飞啊飞 ");
}
public void FlyTwo()
{
Console.WriteLine("飞啊飞 ");
}
public FlyDuck(string _id, string _color, float _weight)
{
this.id = _id;
this.color = _color;
this.weight = _weight;
}
}
class Program
{
[STAThread]
static void Main(string[] args)
{
DK[] dks = new DK[100];
for (int i = 0; i < dks.Length; i++)
{
//初始化鸭子数组,数据在使用前要初始化
dks[i] = new Duck("0", "0", 0.0f);
}
dks[0] = new Duck("A01", "DK1", 1.2f);
dks[1] = new FlyDuck("A02", "DK1", 1.2f);
dks[2] = new Duck("A03", "DK1", 1.2f);
dks[3] = new FlyDuck("A04", "DK1", 1.2f);
dks[4] = new Duck("A05", "DK1", 1.2f);
dks[5] = new Duck("A06", "DK6", 1.2f);
foreach (DK it in dks)
{
//让会飞的鸭子飞
if (it is FlyDuck)
{
//Console.WriteLine(it.id);
((IFly)it).Fly();
}
//if (it is Duck) {
// Console.WriteLine(it.id);
//}
}
Console.ReadLine();
}
}
}
C# 接口的综合例子
猜你喜欢
转载自blog.csdn.net/CQL_K21/article/details/87566906
今日推荐
周排行