我需要Xml序列化的单独类。我想知道继承 BuildingDetail 类是否有更简单的方法从父 Building 类中获取属性值。
父类
public class Building
{
public int BuildingId;
public string BuildingName;
protected Building()
{
}
private Building(int buildingId, string buildingName)
{
BuildingId = buildingId;
BuildingName = buildingName;
}
public static Building Load(int buildingId)
{
var dr = //DataRow from Database
var building = new Building(
(int) dr["BuildingId"],
(string) dr["BuildingName"]);
return building;
}
}
继承课程
public class BuildingDetail : Building
{
public BaseList<Room> RoomList
{
get { return Room.LoadList(BuildingId); }
}
protected BuildingDetail()
{
}
// Is there a cleaner way to do this?
private BuildingDetail(int buildingId, string buildingName)
{
BuildingId = buildingId;
BuildingName = buildingName;
}
public new static BuildingDetail Load(int buildingId)
{
var building = Building.Load(buildingId);
var buildingDetail = new BuildingDetail(
building.BuildingId,
building.BuildingName
);
return buildingDetail;
}
}
感谢。
答案 0 :(得分:5)
首先,将基类构造函数访问修饰符更改为protected
。然后,您可以使用base
关键字调用基类构造函数:
private BuildingDetail(int buildingId, string buildingName)
: base(buildingId, buildingName)
{
...
}
它将首先调用基础构造函数。此外,如果你没有在构造函数之后放置:base(param1, param2)
,那么将调用base的空构造函数。