Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 访问对象图中的根类封装属性_C#_Oop_Encapsulation - Fatal编程技术网

C# 访问对象图中的根类封装属性

C# 访问对象图中的根类封装属性,c#,oop,encapsulation,C#,Oop,Encapsulation,我目前正在编写一个冒险游戏创建者框架,目前为止我有以下课程: // 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() {

我目前正在编写一个冒险游戏创建者框架,目前为止我有以下课程:

// 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; }
}
//表示完整游戏中单个情节的基类。
公共抽象类插曲:IEpisode
{
公共房间列表房间{get;}
公共人工制品列表人工制品{get;}
公共插曲()
{
房间=新房间列表();
人工制品=新人工制品列表();
}
}
//这是本集中所有对象的列表。
公共艺术品列表:列表
{
公共IARTEACT创建(UInt32 id,字符串文本)
{
IArtefact艺术=新的人工制品(id,文本);
添加(艺术);
回归艺术;
}
}
//这是本集所有房间的列表。
公共房间列表:列表
{   
公共IRoom创建(UInt32 id,字符串文本)
{
IRoom rm=新房间(id,文本);
基础。添加(rm);
返回rm;
}
}
公共教室:IRoom
{
公共UInt32 Id{get;set;}
公共字符串文本{get;set;}
公共文物
{
得到
{
return???什么???//如何从访问Artifacts属性
//基类:
//(房间-->房间列表-->插曲)
}
}   
}
公共类人工制品:IArtefact
{
公共UInt32 Id{get;set;}
公共字符串文本{get;set;}
公共IRoom CurrentRoom{get;set;}
}
公共接口协议
{
UInt32 Id{get;set;}
字符串文本{get;set;}
IRoom CurrentRoom{get;set;}
}
公共接口IRoom
{
UInt32 Id{get;set;}
字符串文本{get;set;}
IList人工制品{get;}
}

我想知道的是
房间
类应该如何访问
插曲
类的封装的
人工制品
属性,而不必在对象图中一直传递对
插曲
的引用,例如,
插曲
-->
房间列表
-->
房间

房间和艺术品之间存在一对多的关系。因此,必须在RoomList中初始化此关系。创建方法:

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执行同样的操作。创建为Artefact需要IRoom实例。

我不想在
Create
方法中包含
ArtefactList
;我不希望呼叫者看到这一点,所以我必须将其放入
RoomList
类的构造函数中。是的,这也是一个选项。