C#中接口的理解

1.C#中对于接口的定义使用关键字interface

2.接口中的方法都没有方法体。必须在实现它的类中实现方法体

3.接口没有构造函数,也没有字段

4.接口及接口中的方法必须定义为public

5.接口名一般习惯上使用大写的I+自定义名命名

6.接口可以继承接口,如下面代码中的IB接口,继承了IA接口



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

namespace ConsoleApplication2
{
    interface IA//定义一个接口IA
    {
        void MethodA();
    }

    interface IB : IA//定义一个接口IB继承另一个接口IA
    {
        void MethodB();
    }

    class Bird : IB//一个类Brid,实现了接口IB,IB又继承了IA,则类Brid必须同时实现了IA和IB两个接口中的所有方法
    {
        public void MethodA()
        {
            Console.WriteLine("实现了IA接口中的MethodA方法");
        }

        public void MethodB()
        {
            Console.WriteLine("实现了IB接口中的MethodB方法");
        }

    }


    class Program
    {
        static void Main(string[] args)
        {

            Bird bird = new Bird();//使用父类声明,子类构造(实例化)
            bird.MethodA();
            bird.MethodB();
            Console.ReadLine();
        }
    }
}



强烈向大家推荐一个好网站,http://www.51zxw.net/study.asp?vip=13417828[我要自学网]。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值