有没有办法在抽象方法中将抽象类型定义为参数,并且在派生类中实现该方法时,您是否更改方法的类型以接受派生类型?
码:
public abstract class ProductBase
{
}
public class SomeProduct
: ProductBase
{
}
public abstract class A
{
protected abstract void addProduct(ProductBase p);
}
// This works
public class B : A
{
protected override void addProduct(ProductBase p)
{
// Do some work
}
}
// This is what I'd like to do
public class C : A
{
// Compiler error here because I have accepted a SomeProduct and not a ProductBase
protected override void addProduct(SomeProduct p) …Run Code Online (Sandbox Code Playgroud)