C#学习笔记之多态之接口

<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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值