C#学习笔记_接口Interface

接口定义了所有类继承接口时应遵循的语法合同。接口定义了语法合同 "是什么" 部分,派生类定义了语法合同 "怎么做" 部分。

接口定义了属性、方法和事件,这些都是接口的成员。接口只包含了成员的声明。成员的定义是派生类的责任。接口提供了派生类应遵循的标准结构。接口使得实现接口的类或结构在形式上保持一致。

抽象类在某种程度上与接口类似,但是,它们大多只是用在当只有少数方法由基类声明由派生类实现时。

接口本身并不实现任何功能,它只是和声明实现该接口的对象订立一个必须实现哪些行为的契约。抽象类不能直接实例化,但允许派生出具体的,具有实际功能的类。

接口声明

接口声明使用关键字interface,它与类的声明类似。接口声明默认是 public 的。下面是一个接口声明的实例:

    interface IMyCommunication  //声明接口IMyCommunication 
    {
        void communicate(); 
    }

 以上实例声明了接口IMyCommunication,接口名一般以大写字母I开头,这个接口只有一个方法communicate,没有参数与返回值,继承本接口的类中定义该方法必须同样是无参数无返回值。本接口应用在完整代码中,如下实例:

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

namespace code
{
    interface IMyCommunication  //定义接口IMyCommunication 
    {
        void communicate(); 
    }

    public class communication:IMyCommunication
    {
        string words;
        public communication(string words)
        {
            this.words = words;
        }
        public void communicate()
        {
            Console.WriteLine(words);
        }
    }

    public class Caller
    {
        public static void Main(string[] args)
        {
            communication com = new communication(Console.ReadLine());
            com.communicate();
            Console.ReadLine();
        }
    }
}

输出结果如下: 

>>>123456
123456 

 多重继承

接口与类不同,一个接口可以继承多个接口,且一个类同样可以继承多个接口。下面的实例可以体现:

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

namespace code
{
    interface IMyCommunication1
    //定义接口IMyCommunication1
    {
        void communicate1(); 
    }

    interface IMyCommunication2: IMyCommunication1
    //定义接口IMyCommunication2,继承自接口IMyCommunication1
    {
        void communicate2();
    }

    interface IMyCommunication3: IMyCommunication1
    //定义接口IMyCommunication3,继承自接口IMyCommunication1
    {
        void communicate3();
    }

    interface IMyCommunication4 :IMyCommunication2, IMyCommunication3
    //定义接口IMyCommunication4,继承自接口IMyCommunication2、IMyCommunication3
    //此处接口IMyCommunication4继承多个其他接口
    {
        void communicate4();
    }
    interface IMyCommunication5  //定义接口IMyCommunication5
    {
        void communicate5();
    }

    public class communication:IMyCommunication4,IMyCommunication5
    //此处communication类继承了多个接口
    {
        string words;

        public communication(string words)
        {
            this.words = words;
        }
        //定义继承的接口中声明的方法
        public void communicate1()
        {
            Console.WriteLine("1." + words);
        }
        public void communicate2()
        {
            Console.WriteLine("2." + words);
        }
        public void communicate3()
        {
            Console.WriteLine("3." + words);
        }
        public void communicate4()
        {
            Console.WriteLine("4." + words);
        }
        public void communicate5()
        {
            Console.WriteLine("5." + words);
        }
    }

    public class Caller
    {
        public static void Main(string[] args)
        {
            communication com = new communication("接口继承");
            com.communicate1();
            com.communicate2();
            com.communicate3();
            com.communicate4();
            com.communicate5();
            Console.ReadLine();
        }
    }
}

其中,接口IMyCommunication2、IMyCommunication3继承自接口IMyCommunication1,而IMyCommunication4继承了接口IMyCommunication2、IMyCommunication3,类communication则继承了接口IMyCommunication4、IMyCommunication5,因此此时类communication必须实现5个接口中声明的所有方法。此时实例输出结果:

1.接口继承
2.接口继承
3.接口继承
4.接口继承
5.接口继承

当一个类或结构实现了给定的接口,那么类或结构的实例可隐式转换为该接口类型。例如上面的实例,Main函数这样修改:

        public static void Main(string[] args)
        {
            communication com = new communication("接口继承");
            IMyCommunication5 c5 = com; //将类实例直接赋给接口类型实例
            c5.communicate5();
            Console.ReadLine();
        }

此时c5即com赋值得到的接口类型实例。

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值