我目前正在撰写冒险游戏创建者框架,到目前为止我有以下课程:
// Base class that represents a single episode from a complete game.
public abstract class Episode : IEpisode
{
public RoomList Rooms {get; }
public ArtefactList Artefacts {get; }
public Episode()
{
Rooms = new RoomList();
Artefacts = new ArtefactList();
}
}
// This is a list of all objects in the episode.
public ArtefactList : List<IArtefact>
{
public IArtefact Create( UInt32 id, String text )
{
IArtefact art = new Artefact( id, text );
base.Add( art );
return art;
}
}
// This is a list of all rooms in the episode.
public RoomList : List<IRoom>
{
public IRoom Create( UInt32 id, String text )
{
IRoom rm = new Room( id, text );
base.Add( rm );
return rm;
}
}
public class Room : IRoom
{
public UInt32 Id { get; set; }
public String Text { get; set; }
public IList<IArtefact> Artefacts
{
get
{
return ???what??? // How do I access the Artefacts property from
// the base class:
// (Room --> RoomList --> Episode)
}
}
}
public class Artefact : IArtefact
{
public UInt32 Id { get; set; }
public String Text { get; set; }
public IRoom CurrentRoom { get; set; }
}
public interface IArtefact
{
UInt32 Id { get; set; }
String Text { get; set; }
IRoom CurrentRoom { get; set; }
}
public interface IRoom
{
UInt32 Id { get; set; }
String Text { get; set; }
IList<IArtefact> Artefacts {get; }
}
我想知道的是Room
类应该如何访问Artefacts
类的封装Episode
属性,而不必将引用传递给Episode
所有沿着对象图的方式,即Episode
- &gt; RoomsList
- &gt; Room
。
答案 0 :(得分:1)
房间和人工制品之间存在一对多的关系。因此,您必须在RoomList.Create方法中初始化此关系:
public IRoom Create( UInt32 id, String text , ArtefactList artefacts)
{
IRoom rm = new Room( id, text , artefacts);
base.Add( rm );
return rm;
}
创建房间时,您可以这样做:
var episode = new Episode();
episode.Rooms.Create(1, "RoomText", episode.Artefacts);
你应该对ArtefactList.Create做同样的事情,因为Artefact需要IRoom实例。