如何在 C# 类中拥有父属性?
例如,在以下类中,我需要在 Cupboard
和 Shelf
中有一个 Parent
属性。我该怎么做?
public class Room
{
public List<Cupboard> Cupboards { get; set; }
}
public class Cupboard
{
public Room Parent
{
get
{
}
}
public List<Shelf> Shelves { get; set; }
}
public class Shelf
{
}
For example, in the following classes I need to have a Parent
property in Cupboard
and Shelf
. How do I do it?
public class Room
{
public List<Cupboard> Cupboards { get; set; }
}
public class Cupboard
{
public Room Parent
{
get
{
}
}
public List<Shelf> Shelves { get; set; }
}
public class Shelf
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用自动实现的属性:
您还可以选择将 setter 设为私有并在构造函数中设置它。
用法:
如果您有许多对象都有一个父房间,您可能需要创建一个接口,以便您可以找出哪个房间是对象的父房间,而不必知道其具体类型。
You can use an automatically implemented property:
You can also choose to make the setter private and set it in the constructor.
Usage:
If you have many objects that all have a parent room you might want to create an interface so that you can find out which room is an object's parent without having to know its specific type.