2 回答

TA貢獻(xiàn)1851條經(jīng)驗 獲得超5個贊
不可以,您不能將基本屬性的類型更改為返回不同的(派生的)類型。
如果不需要繼承,則采用標(biāo)準(zhǔn)解決方法-通用類:
public class ParentClass<T> {
public T ItemValue { get; set; }
...
}
public class ChildClass : ParentClass<ChildClass>
{
...
}
請注意,如果您只需要訪問自己類中的item,則可以擁有virtual屬性:
public class Parent { }
public class Child:Parent { public string ChildProperty; }
public abstract class ParentClass
{
public abstract Parent ItemValue { get; }
}
public class ChildClass : ParentClass
{
Child item;
public override Parent ItemValue { get {return item;} }
public void Method()
{
// use item's child class properties
Console.Write(item.ChildProperty);
}
}
- 2 回答
- 0 關(guān)注
- 188 瀏覽
添加回答
舉報