.NET中的三种接口实现方式

一般来说.NET提供了三种不同的接口实现方式,分别为隐式接口实现、显式接口实现、混合式接口实现。这三种方式各有各的特点。

首先来看隐式接口实现,这恐怕是我们使用最多的一种接口实现,因为隐匿接口实现是.NET的默认接口实现方式。下面让我们来看一个隐式接口实现的例子:

using System;
internal class MyClass
{
	public void SomeMethod()
      {
		// 利用接口的方式声明一个Myinplement对象
		IMyInterface iObj = new MyInplement();
	     iObj.MethodA();
		// 利用类的方式声明一个Myinplement对象
		MyInplement obj = new MyInplement();
	     obj.MethodB();
      }
}
public class MyInplement : IMyInterface
{
	#region IMyInterface Members
	/// <summary>
	/// 利用隐式接口实现方式实现接口中的方法
	/// </summary>
	public void MethodA()
	{
		Console.WriteLine("a");
	}
	/// <summary>
	/// 利用隐式接口实现方式实现接口中的方法
	/// </summary>
	public void MethodB()
	{
		Console.WriteLine("B");
	}
}
public interface IMyInterface
{
	void MethodA();
	void MethodB();
}

上面的代码很简单,我们声明了一个接口,然后利用MyImplement类来实现接口。在实现接口的时候我们选择了隐式接口实现,即在接口中定义的方法签名前加上修饰符来定义实现方法的签名,比如public voidMethodB()。这种隐式接口实现用起来很简单,但却有一个缺点,就是它允许客户可以直接调用实现类中的方法,(即通过MyInplementobj = newMyInplement()这样的方式声明对象)而不是通过接口调用实现类中的方法(即通过的IMyInterface iObj = newMyInplement();方式来声明对象),这无疑增加了代码的耦合性和违反了面向接口编程的原则。那怎么样才能禁止客户直接调用实现类呢? 下面我们看的显式接口实现就可以。

显式接口实现是指实现类在实现接口的方法时在方法的签名上用"接口名.方法名”的样式来定义方法名,同时在方法的签名中不允许带有修饰符,因为所有显式接口实现的方法全是private的,这是.NET为了禁止客户直接调用实现类而硬性规定的。下面我们来看显式接口实现的例子:

using System;
internal class MyClass
{
	public void SomeMethod()
	{
		// 利用接口的方式声明一个Myinplement对象
		IMyInterface iObj = new MyInplement();
		iObj.MethodA();
		// 利用类的方式声明一个Myinplement对象,会报错,因为MyImplement的MethodA方法是private的
		MyInplement obj = new MyInplement();
		obj.MethodB();
	}
}
public class MyInplement : IMyInterface
{
	#region IMyInterface Members
	/// <summary>
	/// 利用显式接口实现方式实现接口中的方法
	/// </summary>
	void IMyInterface.MethodA()
	{
		Console.WriteLine("a");
	}
	/// <summary>
	/// 利用显式接口实现方式实现接口中的方法
	/// 会报错,因为在显式接口实现时不允许带有修饰符
	/// </summary>
	public  void IMyInterface.MethodB()
	{
		Console.WriteLine("B");
	}
	#endregion
}
public interface IMyInterface
{
	void MethodA();
	void MethodB();
}

上面的代码中有两个地方会报错,这是因为我们在报错的地方违反了显式接口实现的约束。

最后让我们来看混合式接口实现,混合式接口实现就是将隐式接口实现和显式接口实现搭在一起使用,下面让我们来看一个例子:

using System;
internal class MyClass
{
	public void SomeMethod()
	{
		// 利用接口的方式声明一个Myinplement对象
		IMyInterface iObj = new MyInplement();
		iObj.MethodA();
		// 利用类的方式声明一个Myinplement对象,不会报错,因为MyImplement的MethodA方法是public的
		MyInplement obj = new MyInplement();
		obj.MethodB();
	}
}
public class MyInplement : IMyInterface
{
	#region IMyInterface Members
	/// <summary>
	/// 利用显式接口实现方式实现接口中的方法
	/// </summary>
	void IMyInterface.MethodA()
	{
		Console.WriteLine("a");
	}
	/// <summary>
	/// 利用隐式接口实现方式实现接口中的方法
	/// </summary>
	public  void MethodB()
	{
		Console.WriteLine("B");
	}
	#endregion
}
public interface IMyInterface
{
	void MethodA();
	void MethodB();
}

上面的代码中我们将MehtodB的实现改为隐式接口实现,这样MyImplement.Method的修饰符就是public的了,代码中就没有错了。

通过简单的代码示例看到了三种接口实现方式的区别,话说在今天之前我一直没有注意到隐式接口实现可以允许实现类直接被调用,而显式接口实现由于实现类中的方法是私有的所以不能被直接调用。看来我对.NET的认识还很初级啊,有许多基础性的知识还没有掌握啊。以后要努力了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值