实现接口与显式实现接口的区别

原文:http://www.cnblogs.com/chenxizhang/archive/2008/08/22/1274265.html

在实现接口的时候,VS提供了两个菜单,一个是"实现接口",一个是"显式实现接口",它们到底有何不一样呢

我们来比较一下看看

1.首先假设我们有一个接口

public interface ICustomer
{
    void SomeMethod();
}

2.如果是"实现接口",那么代码大致如下

public class Customer:ICustomer
{

#region ICustomer 成员

public void SomeMethod()
    {
        throw new NotImplementedException();
    }

#endregion
}

3.如果是"显式实现接口",那么代码大致如下

public class Customer:ICustomer
{

#region ICustomer 成员

void ICustomer.SomeMethod()
    {
        throw new NotImplementedException();
    }

#endregion
}

大家看到差别了吧?显式实现的方式,那些方法都会加上一个前缀的。但是,这到底意味着什么呢?

如果是实现接口

public class DAL {
    /// <summary>
    /// 如果我们是直接实现接口的话,那么既可以用接口调用方法,也可以用具体类调用方法
    /// </summary>
    public void GetCustomer() {
        Customer customer = new Customer();
        customer.SomeMethod();
    }

public void GetCustomer2() {
        ICustomer customer = new Customer();
        customer.SomeMethod();
    }
}

如果是显式实现接口

public class DAL {
    /// <summary>
    /// 如果我们是显式实现接口的话,那么要访问里面的方法就只能是通过接口来调用,而不能通过具体类来做
    /// </summary>
    public void GetCustomer() {
        ICustomer customer = new Customer();
        customer.SomeMethod();
    }
}

现在大部分的系统为了保证扩展性,都广泛地使用接口。显式实现接口,可以隐藏具体类的复杂性。

时间: 2024-08-27 18:06:19

实现接口与显式实现接口的区别的相关文章

基类显式继承接口,类继承基类时又继承同一接口,引发接口方法混乱(显式继承接口的弊端)

基类BaseOutput显式继承了一个接口IOutput,之后类TrackOutput继承BaseOutput,同一时候又继承了IOutput接口.假定IOutput有方法Output,这样在TrackOutput中就有两个Output方法,一个源于基类BaseOutput,于个源于接口IOutput.这样就引发了混乱.要怎么办?先看以下的一段代码 interface IOutput { void output(); } class BaseOutput : IOutput { void IOu

C#显式实现接口

如何显示实现一个接口?坐好看图: 接口的显式实现是一种约束,它限制只有此接口的实例才能直接调用此接口内被显示实现的方法: 其实我们可以脑补一个杀手,这个杀手可以转换自己身份,平日里不会杀人,这时他的身份是WarmKiller,但当他遇到敌人,他就把自己的身份转换成Killer去干了敌人. 总的来说,只有当此人身份(实例)是Killer时,他才能kill. 原文地址:https://www.cnblogs.com/kekekuli/p/12641993.html

显示实现接口和隐式实现接口区别

先定义一个接口: public Interface ICategory { string GetName(); } 接着实现上面定义的接口: public class Category:ICategory { public string GetName() { return name; } } 以上的方式是隐式的实现接口(我们默认的都是使用隐式的接口) 显式的实现接口方式: public class Category:ICategory { public string  ICategory.Ge

接口的显式实现与隐式实现

我们在实现接口时,常常是类继承接口,然后在接口中实现相应的方法.代码如下: interface IOutput { void output(); } class SimpleOutput : IOutput { public void output() { Console.WriteLine("SimpleOutput ....."); } } 使用代码测试一下实现的结果 class Program { static void Main(string[] args) { SimpleO

008.在C#中,显式接口VS隐式接口

原文http://www.codeproject.com/Articles/1000374/Explicit-Interface-VS-Implicit-Interface-in-Csharp (Aty表示本人) 介绍 什么是显式和隐式接口 什么时候需要显式接口 更简洁的方法(ISP:接口隔离原则) 显式接口更多 结论 介绍 文章将讨论下显式接口,讨论它们与隐式接口的区别,以及为什么应该避免使用. 什么是显式和隐式接口 C#中有两种方式来实现接口:显式和隐式 定义如下接口: 当我们隐式的实现该接

类继承多个接口有相同的方法时,怎么办?(接口显式实现的作用)

在<接口的显式实现与隐式实现>中讲到了接口的显式实现,那有什么作用呢?我们来看一段代码. class Program { static void Main(string[] args) { SimpleOutput s = new SimpleOutput(); IOutput io = s; ILog il = s; s.output(); io.output(); il.output(); Console.ReadLine(); } } interface IOutput { void o

简单的interface显式和隐式的实现

一,新建接口 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// InterF 的摘要说明 /// </summary> public interface Itest { string show(); string display(); } 二,新建接口实现类 using System; using System.Collecti

C#的接口基础教程之五 实现接口

1.显式实现接口成员 为了实现接口,类可以定义显式接口成员执行体(Explicit interface member implementations).显式接口成员执行体可以是一个方法.一个属性.一个事件或者是一个索引指示器的定义,定义与该成员对应的全权名应保持一致. using System ;interface ICloneable { object Clone( ) ;}interface IComparable { int CompareTo(object other) ;}class

1.扩展方法2.接口的隐式实现和显式实现

1.扩展方法:必须写在一个静态类里面,具体见代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {             Student s = new Stud