我理解在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();
在IT技术中,为什么要让一个类(例如Human)实现一个接口,并将其实例human强制转换回接口?这个问题不是如何实现,而是为什么这样做。
baseObject is IInterface
进行检查。有趣的是方法参数本来就没有被定义为IInterface
类型。 - Jodrell