41、C#:C#中对于接口的实现方式

第一部分:本文来自:https://www.cnblogs.com/summer1987/p/4600100.html

C#中对于接口的实现方式有隐式接口和显式接口两种:

隐式地实现接口成员
创建一个接口,IChinese,包含一个成员 Speak;我们创建一个类Speaker,实现接口Chinese

复制代码
//隐藏式实现例子
public interface IChinese
{
    string Speak();
}
public class Speaker : IChinese
{
    public string Speak()
    {
        return "中文";
    }
}
复制代码

这个就是隐式实现接口。

隐式实现调用方法如下:

IChinese s = new Speaker();
s.Speak();

Speaker s = new Speaker();
s.Speak();

都可以调用Speak这个方法。

创建一个接口,IEnglish,包含一个成员 Speak;让我们的类Speaker来实现接口IEnglish

复制代码
//显式实现例子
public interface IEnglish
{
    string Speak();
}
public class Speaker : IEnglish
{
     string English.Speak()
    {
        return "English";
    }
}
复制代码

通过这种显示接口实现。Speak方法就只能通过接口来调用:

IEnglish s = new Speaker();
s.Speak(); 

下面的这种方式将会编译错误: 

Speaker s = new Speaker();
s.Speak(); 

隐式实现和显示实现的区别:

一、语法的区别

1、隐式方式Speaker的成员(Speak)实现有而且必须有自己的访问修饰符(public),显示实现方式Speaker的成员(Speak)不能有任何的访问修饰符。
2、显示实现方式Speaker使用接口名称和一个句点命名该类成员(Speak)来实现的:English.Speak();也就是
二、调用的区别
隐式接口实现的调用,注意类的声明,可以用接口声明,也可以用实现类 Speaker声明。调用者都可以得到调用实例化对象的行为Speak;

复制代码
class Program
{
    static void Main(string[] args)
    {
        IChinese s = new Speaker();
        s.Speak();
        Speaker s = new Speaker();
        s.Speak();
    }
} 
复制代码

显式接口实现调用,注意类的声明,只可以用接口声明,调用者才可以可以得到调用实例化对象的行为Speak;   

复制代码
class Program
{
        static void Main(string[] args)
        {
            IEnglish s = new Speaker();
            s.Speak();
           //下面写法将引起编译错误错误“PetShop.Speaker”不包含“Speak”的定义;
           // Speaker s = new Speaker();
           // s.Speak();
        }
}
复制代码

结论:
隐示实现对象声明为接口和类都可以访问到其行为,显示实现只有声明为接口可以访问。

如果两个接口中有相同的方法名,那么同时实现这两个接口的类,就会出现不确定的情形,在编写方法时,也不知道实现哪个接口的方法了。为解决这一问题,C#提供了显示接口实现技术,就是在方法名前加接口名称,用接口名称来限定成员。用“接口名.方法名()”来区分实现的是哪一个接口。

注意:显示接口实现时,在方法名前不能加任何访问修饰符。这种方式和普通方法不同,普通方法前不加访问修饰符,默认为私有的,而显式接口实现时方法前不加任何修饰符,默认为公有的,如果前面加上修饰符,会出现编译错误。

调用显示接口实现的正确方式是通过接口引用,通过接口引用来确定要调用的版本。

下面我们看一下完整实例:

复制代码
using System;
public interface IPerson
{
  string Name
  {
      get;
      set;
  }
  void Show(string name);
}

public interface IStudent
{
  string StudentId
  {
      get;
      set;
  }
  void Show(string studentid);
}

public class Student: IPerson, IStudent
{
  private string _name;
  public string Name
  {
      get
      {
        return _name;
      }
      set
      {
        _name = value;
      }
  }

  private string _studentid;
  public string StudentId
  {
      get
      {
        return _studentid;
      }
      set
      {
        _studentid = value;
      }
  }

  void IPerson.Show(string name)
  {
      Console.WriteLine("姓名为{0}", name);
  }

  void IStudent.Show(string studentid)
  {
      Console.WriteLine("学号为{0}", studentid);
  }
}

class Program
{
  static void Main()
  {
      Student s = new Student();
      Console.WriteLine("输入姓名");
      s.Name = Console.ReadLine();
      Console.WriteLine("输入学号");
      s.StudentId = Console.ReadLine();
      IPerson per = s;  //这里如果使用s.Show(...)编译错误,提示找不到方法定义,因为使用了:接口名:方法名 的格式,此方法不属于student类(此行注释不属于原作者)
      per.Show(s.Name);
      IStudent stu = s;
      stu.Show(s.StudentId);
  }
}

第二部分:本文来自:https://zhidao.baidu.com/question/510903230.html

下面是较常用的使用方法
using System;
namespace ClassLibrary2
{
  interface IEmploy //接口
  {
    void Speak(); //方法
  }
  class Hello:IEmploy //Hello类实现接口
  {
    public void Speak() //实现方法
    {
      Console.WriteLine("Hello:朋友");
    }
  }
  class Sorry:IEmploy //Sorry类实现接口
  {
    public void Speak() //实现方法
    {
      Console.WriteLine("Sorry:朋友");
    }
  }
}
实现
//直接调用
IEmploy Ie = new Hello();
Ie.Speak(); //调用Hello类实现的接口
IEmploy Ie = new Sorry();
Ie.Speak();
//反射调用 记住反射的空间引用 using System.Reflection;
Assembly Asm = Assembly.Load("ClassLibrary2");//反射出空间
Type type = Asm.GetType("ClassLibrary2.Hello");//反射出空间下的类
object AssClas = Activator.CreateInstance(type);//动态实力化反射回来的指定空间下的指定类
IEmploy Ie = (IEmploy)AssClas; ////转换为接口类型
//常用的就这几种方法

猜你喜欢

转载自blog.csdn.net/xushaozhang/article/details/78662276