[英]What is interface casting for in C#?
我確實理解如何編寫一個接口在C#中工作,例如這里描述的: codeguru解釋
interface IIntelligence
{
bool intelligent_behavior();
}
class Human: IIntelligence
{
public Human()
{
//.............
}
/// Interface method definition in the class that implements it
public bool intelligent_behavior()
{
Console.WriteLine("........");
return true
}
}
但是我對以下接口轉換過程感到困惑:
Human human = new Human();
// The human object is casted to the interface type
IIntelligence humanIQ = (IIntelligence)human;
humanIQ.intelligent_behavior();
有一個類(在這種情況下是Human )實現一個接口,然后將它的實例人體反饋到接口的意義是什么? 問題不在於它是如何運作的,而是為什么要這樣做。
.net提供了兩種類型的接口實現隱式實現和顯式實現。
當您使用隱式實現時,它將成為類型接口本身的一部分,例如,如果您有這樣的IPerson接口:
public interface IPerson
{
string Name{get;}
}
並按如下方式實現:
public class Person:IPerson
{
public string Name{get; ....}
}
您可以像這樣(隱式)訪問它:
aPerson.Name;
但如果你這樣(明確地)實現它:
public class Person:IPerson
{
string IPerson.Name{get; ....} // notice that there's no need to include access modifier.
}
然后它只能使用IPerson接口訪問:
((IPerson)aPerson).Name;
更新:
實際上,顯式接口實現允許我們使用具有相同名稱的成員實現不同的接口。(如本教程所示)
有時您可能不知道對象是什么,但您知道它實現了某個接口。
我發現在為主應用程序開發插件時,轉換為接口非常有用。 我創建了三個項目。 第一個項目'connectorInterface'只包含一個接口的類定義。 接口代碼:
public interface IConnectorDataReader
{
int ColumnCount
{
get;
}
bool readNextRecord();
string this[int i]
{
get;
}
void reset();
}
第二個項目'dataSource1'(主應用程序的插件)實現了IConnectorDataReader接口,實現接口的類也有一些額外的私有方法。 使用插件'dataSource1'時,第三個項目'main application'使用此代碼從插件'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) ....
在我的情況下,轉換對於讀取插件數據很有用。 只要它實現了通用接口,我不關心插件是如何實現的。 我關心和使用的只是讀取數據的常用方法。 我認為接口很有用,並且還可以返回接口。
這是訪問顯式接口實現。
有時您想要隱藏類實現接口的事實。 這是通過顯式實現接口來完成的。
public class MyClass : IInterface
{
string IInterface.TheMethod(){}
}
出於同樣的原因,您將派生類強制轉換回它的基類。
一個簡短而流行的例子。 我們可以實現這樣的代碼:interface IIntelligence {string Talk(); }
class Cat: ICreature
{
public string Talk()
{
Console.WriteLine("Meow!");
return true
}
}
class Dog: ICreature
{
public string Talk()
{
Console.WriteLine("Arf!");
return true
}
}
class Human: ICreature
{
public string Talk()
{
Console.WriteLine("Hello!");
return true
}
}
然后我們可以使用以下代碼:
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
以獲得您想要的方法實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.