C# 对泛型如何与继承一起工作感到困惑
我试图在工作中重构一些代码,但遇到了一些问题。假设我有以下代码(为了说明问题而大大简化): 抽象行类:C# 对泛型如何与继承一起工作感到困惑,c#,C#,我试图在工作中重构一些代码,但遇到了一些问题。假设我有以下代码(为了说明问题而大大简化): 抽象行类: abstract class Row { } 扩展行的具体行类 class SpecificRow : Row { } 采用泛型类型和接受ICollection的方法的接口: interface IDbInsertable<T> { void InsertToDb(ICollection<T> list); } 接口可插入 { void Inse
abstract class Row
{
}
扩展行的具体行类
class SpecificRow : Row
{
}
采用泛型类型和接受ICollection的方法的接口:
interface IDbInsertable<T>
{
void InsertToDb(ICollection<T> list);
}
接口可插入
{
void InsertToDb(i收集列表);
}
实现上述接口的抽象类:
abstract class BaseDownloader: IDbInsertable<Row>
{
public abstract void InsertToDb(ICollection<Row> list);
//and other unrelated methods...
}
抽象类BaseDownloader:IDbInsertable
{
公开摘要无效插入TODB(ICollection list);
//和其他不相关的方法。。。
}
扩展BaseDownloader的具体类:
class SpecificDownloader : BaseDownloader
{
public void InsertToDb(ICollection<SpecificRow> list)
{
//implementation
}
//other stuff
}
class SpecificDownloader:BaseDownloader
{
公共void InsertToDb(ICollection列表)
{
//实施
}
//其他东西
}
在SpecificDownloader类中,我得到错误“SpecificDownloader未实现继承的抽象成员'BaseDownloader.InsertToDb(ICollection)
”
我所尝试的:
公共无效
InsertToDb()
到公共覆盖无效InsertToDb()
,在这种情况下
错误消息变为“SpecificDownloader.InsertToDb不适用”
找到要重写“”的方法李>
从理论的角度来看,我认为上面的内容应该很好,但它不允许我编译,我也没有理由这么做。如果我遗漏了一些重要的内容,请告诉我。将BaseDownloader设置为通用类。并添加一个类型约束,强制T成为行的类型。像这样
//Class implements the interface and uses the Generic type T from basedownloader. And that has the type constraint
abstract class BaseDownloader<T> : IDbInsertable<T> where T : Row
{
//This forces T to always be a type row
public abstract void InsertToDb(ICollection<T> list);
//and other unrelated methods...
}
//Class now gives specificrow as the generic type when inheriting from basedownloader
class SpecificDownloader : BaseDownloader<SpecificRow>
{
//Now InsertToDb has a collection of SpecificRow instead of just row
public override void InsertToDb(ICollection<SpecificRow> list)
{
//implementation
}
//other stuff
}
//类实现接口并使用basedownloader中的泛型类型T。这有类型约束
抽象类BaseDownloader:IDbInsertable,其中T:Row
{
//这将强制T始终为类型行
公开摘要无效插入TODB(ICollection list);
//和其他不相关的方法。。。
}
然后在从basedownloader继承时指定所需的行类型。
像这样
//Class implements the interface and uses the Generic type T from basedownloader. And that has the type constraint
abstract class BaseDownloader<T> : IDbInsertable<T> where T : Row
{
//This forces T to always be a type row
public abstract void InsertToDb(ICollection<T> list);
//and other unrelated methods...
}
//Class now gives specificrow as the generic type when inheriting from basedownloader
class SpecificDownloader : BaseDownloader<SpecificRow>
{
//Now InsertToDb has a collection of SpecificRow instead of just row
public override void InsertToDb(ICollection<SpecificRow> list)
{
//implementation
}
//other stuff
}
//类现在在从basedownloader继承时将specificrow作为泛型类型
类特定下载程序:BaseDownloader
{
//现在InsertToDb有一个SpecificRow的集合,而不仅仅是row
公共覆盖void InsertToDb(ICollection列表)
{
//实施
}
//其他东西
}
更多关于的信息将修复:抽象类BaseDownloader:IDbInsertableJava和C#以不同的方式实现泛型。请选择一个最合适的langauge标记该方法的签名需要是
InsertToDb(ICollection list)
,由抽象类定义(将SpecificRow更改为Row)。接口定义了该类应执行的操作。而特定的下载程序无法按照界面上的说明进行操作。它可以做得更少:只针对特定的事情。你知道extend这个词的意思是做得更多,而不是做得更少,是吗?