从具有其他类型属性的抽象类继承(.NET 3.5、C#)
我有 3 个以下类:
public class BaseProperty1{
public string Property1 {get; set;}
}
public class ChildProperty1 : BaseProperty1 {
}
public abstract class Base{
public abstract BaseProperty1 bp1 {get; set;}
}
我试图从 Base 派生一个以下类:
public class Child : Base{
public ChildProperty1 bp1 {get; set;}
}
但我收到一个错误,指出“set”和“get”方法未实现。这只是我使用的语法还是我的思维方式错误?
谢谢你!
I have 3 following classes:
public class BaseProperty1{
public string Property1 {get; set;}
}
public class ChildProperty1 : BaseProperty1 {
}
public abstract class Base{
public abstract BaseProperty1 bp1 {get; set;}
}
I'm trying to derive a following class from Base:
public class Child : Base{
public ChildProperty1 bp1 {get; set;}
}
But I'm getting an error that "set" and "get" methods are not implemented. Is it just the syntax I'm using or my way of thinking is wrong?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将无法使用自动属性,因为您必须完全匹配基类的类型。您必须采用老式方法:
您也可以使用泛型来解决问题:
You won't be able to use Auto Properties since you have to match the type of the base class exactly. You'll have to go the old fashioned way:
You might also be able to use generics to solve the problem:
如果将方法或属性标记为抽象,则必须在继承类中实现它。
您可以隐藏旧属性(基类中的 bp1)并使用另一种返回类型编写新属性,如下所示:
If you mark method or property as an abstract, you must implement it in inherited class.
You can hide old property (bp1 in Base class) and write new one with another return type like this: