<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 多态之接口
{
class Program
{
static void Main(string[] args)
{
// 1、接口是一种规范,也是一种能力,继承接口之后必须实现接口中的所有方法。
// 2、接口和抽象类一样,不能创建对象。
// 3、接口中的方法不能加访问修饰符,默认是public。
// 4、接口中的方法不能有方法体。
// 5、接口中只能有方法、属性、索引器、事件,不能有字段和构造函数。
// 6、接口与接口之间可以继承,并且可以多继承。
// 7、接口不能继承类,但是类可以继承接口。
// 8、一个类可以在继承一个类的同时并实现多个接口,类必须写在接口前面。
//IFlyable fly = new Persion();
IFlyable fly = new Bird();
fly.Fly();
Console.ReadKey();
}
}
// 声明一个接口,使用关键字 interface
public interface IFlyable
{
// 接口中的方法不允许有访问修饰符
// 接口中的方法不可以有方法体。
void Fly();
}
public class Persion : IFlyable
{
// 继承接口之后必须实现接口中的方法
public void Fly()
{
Console.WriteLine("人类在飞");
}
}
public class Bird : IFlyable
{
public void Fly()
{
Console.WriteLine("鸟在飞");
}
}
}
</span>
C#学习笔记之多态之接口
最新推荐文章于 2024-06-17 20:59:33 发布