似乎找不到解决方案,也不明白为什么这是不可能的:
(第一行代码似乎不想在我的浏览器上正确格式化)
namespace Definitions
{
public interface iCore
{
public ChannelBase GetChannel(UrlBase url);
}
public abstract class Core : iCore
{
public abstract ChannelBase GetChannel(UrlBase url);
}
public class ChannelBase
{
public ChannelBase(iCore core)
{ }
}
public class UrlBase
{
public UrlBase(iCore core)
{ }
}
}
namespace Processing
{
/// Error Thrown:
//Severity Code Description Project File Line Suppression State
//Error CS0534 'MyCore' does not implement inherited abstract member 'Core.GetChannel(UrlBase)'
public class MyCore : Definitions.Core
{
/// It suggests this Implementation:
//public override Definitions.ChannelBase GetChannel(Definitions.UrlBase url) { }
/// I need this to work, but how ?
public override MyChannel GetChannel(MyUrl url)
{
/// This is where i would really need that MyUrl.MyExtraPropertyIneed !
return null;
}
}
public class MyChannel : Definitions.ChannelBase
{
public MyChannel(MyCore core) : base(core)
{ }
}
public class MyUrl : Definitions.UrlBase
{
public MyUrl(MyCore core) : base(core)
{
MyExtraPropertyIneed = "I NEED this property"
}
public string MyExtraPropertyIneed { get; private set; }
// PS this is a basic example, but i really can't have this property in the Base Class, the Derrived class gets info from online and does "stuff"
}
}
“public override MyChannel GetChannel(MyUrl url)”总是抛出错误“‘MyCore’没有实现继承的抽象成员‘Core.GetChannel(UrlBase)’”
如何解决这个问题?
期望派生类被接受 请注意,例如我使用了不同的命名空间,但实际上每个命名空间都是一个单独的项目。
像这样编辑
GetChannel()
的签名
public override ChannelBase GetChannel(UrlBase url)
{
// implementation
return new MyChannel(this);
}
函数覆盖不允许签名不同。如果要返回子类,可能具有与抽象方法相同的签名,但返回子类实例,然后在方法的调用者中转换为子类。
public override ChannelBase GetChannel(UrlBase url)
{
return new MyChannel();
}
In the caller method,
var obj = new MyCore().GetChannel();
这应该有效。