【发布时间】:2014-04-20 21:50:16
【问题描述】:
我确实了解在 C# 中编写接口的工作原理,例如此处所述: codeguru explanation
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)实现一个接口,然后将其实例 human 转换回该接口是什么意思?问题不在于它是如何工作的,而在于为什么这样做。
【问题讨论】: