C#中的界面转换是为了什么?
我确实理解用C#编写接口的工作原理,例如: 然而,我对以下界面铸造过程感到困惑:C#中的界面转换是为了什么?,c#,interface,casting,C#,Interface,Casting,我确实理解用C#编写接口的工作原理,例如: 然而,我对以下界面铸造过程感到困惑: Human human = new Human(); // The human object is casted to the interface type IIntelligence humanIQ = (IIntelligence)human; humanIQ.intelligent_behavior(); 让一个类(在本例中是人)实现一个接口,然后将其实例人重新转换回接口有什么意义?问题不在于它是如何工作
Human human = new Human();
// The human object is casted to the interface type
IIntelligence humanIQ = (IIntelligence)human;
humanIQ.intelligent_behavior();
让一个类(在本例中是人)实现一个接口,然后将其实例人重新转换回接口有什么意义?问题不在于它是如何工作的,而在于它为什么这样做。问题在于获得显式接口实现的访问权限 有时,您希望隐藏类实现接口的事实。这是通过显式实现接口来实现的
public class MyClass : IInterface
{
string IInterface.TheMethod(){}
}
有时您可能不知道对象是什么,但您知道它实现了某个接口。出于同样的原因,您会将派生类强制转换回它的基类。一个简单而流行的示例。我们可以实现这样的代码: 接口智能 { 字符串对话(); } 然后我们可以使用以下代码:
ICreature() creatures = new ICreature(){new Human(), new Dog(), new Cat()};
foreach(IIntelligence creature in creatures){
Console.WriteLine(creature.Talk());
}
有关更多详细信息,请参阅google中的“面向对象编程中的多态性”。考虑这种情况。我在您的示例中添加了一个方法
interface IIntelligence
{
bool intelligent_behavior();
}
class Human: IIntelligence
{
public Human() { }
/// Interface method definition in the class that implements it
public bool IIntelligence.intelligent_behavior()
{
Console.WriteLine("........");
return true;
}
//Some other method definition
public bool intelligent_behaviour()
{
return false;
}
}
您可以使用
IIntelligence
来获得所需的方法实现。.net提供了两种类型的接口实现—隐式实现和显式实现
当您使用隐式实现时,它将成为类型接口本身的一部分,例如,如果您有这样一个IPerson接口:
public interface IPerson
{
string Name{get;}
}
您可以按如下方式实现它:
public class Person:IPerson
{
public string Name{get; ....}
}
您可以这样访问它(隐式):
但如果您这样(明确地)实现它:
则只能使用IPerson接口访问:
((IPerson)aPerson).Name;
更新:
实际上,显式接口实现允许我们使用具有相同名称的成员实现不同的接口。(如中所示)我发现在为主应用程序开发插件时,转换接口很有用。我创建了三个项目。第一个项目“connectorInterface”只包含一个类定义,即inteface。接口代码:
public interface IConnectorDataReader
{
int ColumnCount
{
get;
}
bool readNextRecord();
string this[int i]
{
get;
}
void reset();
}
第二个项目“dataSource1”(主应用程序的插件)实现IConnectorDataReader接口,并且实现该接口的类还有一些额外的私有方法。第三个项目“主应用程序”在使用插件“dataSource1”时使用此代码从插件“dataSource1”读取数据:
Assembly assembly = Assembly.LoadFile(path); // path to dll
Type type = assembly.GetType("dataSource1.DataReader");
object myInstance = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod("getConnectorDataReader");
object data = method.Invoke(myInstance, null);
IConnectorDataReader reader =(IConnectorDataReader)data;
// method usage
while (reader.readNextRecord() == true) ....
在我的例子中,casting对于读取插件数据非常有用。我不在乎插件是如何实现的,只要它实现了公共接口。我所关心和使用的只是读取数据的常用方法。我认为接口是有用的,也可以回溯到接口。可能会发生,我想你应该做一个
baseobjectisiinterface
来检查。奇怪的是,方法参数尚未键入到IInterface
。
public class Person:IPerson
{
string IPerson.Name{get; ....} // notice that there's no need to include access modifier.
}
((IPerson)aPerson).Name;
public interface IConnectorDataReader
{
int ColumnCount
{
get;
}
bool readNextRecord();
string this[int i]
{
get;
}
void reset();
}
Assembly assembly = Assembly.LoadFile(path); // path to dll
Type type = assembly.GetType("dataSource1.DataReader");
object myInstance = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod("getConnectorDataReader");
object data = method.Invoke(myInstance, null);
IConnectorDataReader reader =(IConnectorDataReader)data;
// method usage
while (reader.readNextRecord() == true) ....