C#中接口的使用

一.什么是接口

接口可以理解为对一组方法声明进行的统一命名,但这些方法没有提供任何实现,也就是说,把一组方法声明在一个接口中,然后继承于该接口的类都需要实现这些方法。

二.接口的定义

接口的定义与类的定义非常相似,只是使用的关键字不一样而已,类的定义使用class关键字,而接口使用interface关键字进行定义。在接口中定义方法不能添加任何修饰符,因为接口中的方法默认为public,如果显示的指定了修饰符,则会出现编译时的错误。在接口中除了可以定义方法外,还可以包含属性、事件、索引器或这四类成员(包括方法)类型的任意组合;但接口类型不能包含字段,运算符重载,实例构造函数和析构函数。

三.隐式接口与显示接口的的区别

A.隐式接口没有具体指明实现的的是哪个接口中的方法;而显示接口,它在实现的过程中明确地指出了是哪个接口中的哪个方法,当一个类中实现多个接口;而且这些接口中包含相同返回值,方法名称和参数类型时,隐式接口的实现就会出现命名冲突的问题。

B.显示接口的实现则能解决类似于上述中实现多个接口中方式时,命名冲突的问题。显示接口的实现需要注意以下几个问题:(1).若显示实现接口,方法不能使用任何访问修饰符,显示实现的成员默认为私有;(2).显示实现的成员默认是私有的,所以这些成员

是不能通过类的对象进行访问的。

四.隐式接口的实现

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

namespace Language
{
    interface IChineseable
    {
        void SayHello();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Language
{
    public class Speaking:IChineseable
    {
        public void SayHello()
        {
            Console.WriteLine("你好");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Language
{
    class Program
    {
        static void Main(string[] args)
        {
            Speaking s = new Speaking();
            s.SayHello();
            IChineseable ic = (IChineseable)s;
            ic.SayHello();
            Console.ReadKey();
        }
    }
}

打印结果:

五.显示接口的实现

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

namespace Language
{
    interface IChineseable
    {
        void SayHello();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Language
{
    interface IEnglishable
    {
        void SayHello();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Language
{
    public class Speaking:IChineseable,IEnglishable
    {
        void IChineseable.SayHello()
        {
            Console.WriteLine("你好");
        }

        void IEnglishable.SayHello()
        {
            Console.WriteLine("Hello");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Language
{
    class Program
    {
        static void Main(string[] args)
        {
            Speaking s = new Speaking();
            IChineseable ic = (IChineseable)s;
            ic.SayHello();
            IEnglishable ie = (IEnglishable)s;
            ie.SayHello();
            Console.ReadKey();
        }
    }
}

打印结果:

六.总结

根据上述隐式与显示接口的实现,能够很清晰的明白在什么场合下使用哪种接口。

(1).若采用隐式接口实现时,类和接口都可以访问接口中的方法;而要是采用显示接口实现方式,接口方法只能通过接口来完成访问,因为此时接口方法默认是私有的。

(2).当类实现单个接口时,通常使用隐式接口实现方式,这样类的对象可直接访问接口方法。

(3).当类实现多个接口,并且接口中包含相同的方法名称、参数和返回类型时,则应该使用显示接口实现方式。即使没有相同的方法签名,在实现多个接口时,仍推荐使用显示的方式,这样可以很明确地标识出哪个方法属于哪个接口

转载于:https://www.cnblogs.com/QingYiShouJiuRen/p/11106194.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值