【发布时间】:2017-12-26 10:24:33
【问题描述】:
我有一个定义
的基类public class BaseClass
{
public virtual bool StartUpdate( Interface )
{ some code here }
}
然后,有一个实现接口的类
public ClientClass : Interface {}
在派生类中
public class DerivedClass : BaseClass
{
public override bool StartUpdate( ClientClass )
{ some code... }
}
编译器 (c#) 在 DerivedClass 上引发错误,提示未找到合适的覆盖方法。
我做错了什么?为什么编译器不能注意到ClientClass 实现了Interface?
任何帮助将不胜感激。
编辑:
找到一个“解决方案”
在基类中
public virtual bool StartUpdate( object o )
{
checkInterface( o ); // if not o is Interface, throws an exception
more code...
}
在派生类中
public override bool StartUpdate( object o )
{
// o will always implement Interface
}
这是一团糟,但 c# 喜欢它的方式。
【问题讨论】:
-
请提供带有正确方法签名的实际代码
-
为什么你需要具体的
ClientClass作为参数,而不是interface? -
@marcelo - 为什么要这样使用
object?你失去了拥有特定对象/接口的所有力量......这可以编译,但肯定不是“C#喜欢它”的方式......你甚至看过下面的答案吗?