我有基础教室
public class Room
{
public int width;
public int height;
public int minX;
public int minY;
public Room(int width, int height, int minX, int minY)
{
this.width = width;
this.height = height;
this.minX = minX;
this.minY = minY;
}
public virtual void GenerateRoom()
{
}
}
ProcedualRoom
和PredefinedRoom
这两种房间
public class ProcedualRoom: Room
{
public ProcedualRoomData data;
}
public class PredefinedRoom: Room
{
public PredefinedRoomData data;
}
两个类各自的数据互不相同ProcedualRoomData
和PredefinedRoomData
在其他正在上课的教室里,
ILoad[] objsToLoad = GetComponentsInChildren<ILoad>();
foreach (Room r in procedualDungeon.rooms)
{
foreach (ILoad obj in objsToLoad)
{
obj.Load(r);
}
}
当我加载房间时,我必须像这样进行打字
public void Load(Room room)
{
if (room is PredefinedRoom)
{
Load((room as PredefinedRoom).data);
}
else
{
ProcedualRoom pRoom = (room as ProcedualRoom);
Load(pRoom);
}
}
还有其他避免类型转换的方法吗?
答案 0 :(得分:1)
您可以创建一个RoomData类,并在Room类中添加具有这种类型的文件。 ProcedualRoomData和PredefinedRoomData将从RoomData派生
public class RoomData
{
...
}
public class ProcedualRoomData: RoomData {
...
}
public class PredefinedRoomData: RoomData {
...
}
public class Room
{
public RoomData data;
....
}