我不知道如何编写接口工作在C#中,例如这里描述: codeguru explanation什么是在C#中的接口铸造?
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();
什么(人类在这种情况下)执行一个接口,然后将其实例人类回到接口?问题不在于它是如何工作的,但为什么它完成。
可能会发生,我想你会做'baseObject is IInterface'来检查。好奇的是,方法参数没有被输入到'IInterface'。 – Jodrell 2011-05-11 09:10:41