在类中创建多个属性级别

时间:2015-03-17 20:34:55

标签: c# class

我已经使用了API,这些API似乎是其他实体中的方法实例,而且我还没有能够弄清楚如何为自己重新创建这个方法。例如,如果我有一个名为" House"的类,并且那个房子里有不同的门,我想在其中一个门上放一个窗口,那么使用该实例的代码就会看起来像什么像这样:

House myHouse = New House;
myHouse.Rooms("living room").Doors("front").PlaceWindow("frosted four pane");

我希望我的表达有意义。我已经在VB.Net生活了几个月,我们刚刚开始转回C#。我真的很好奇This.This.This.This方面。

我希望有一个易于使用和操作的对象,我只能用我目前的知识在一个类中放置属性和方法。我只是不知道在搜索引擎中搜索什么来弄清楚如何制作这种类(或者它是什么类型的编译)。如果有人能引导我朝着正确的方向前进,那将会有很大的帮助。

2 个答案:

答案 0 :(得分:2)

方法类型存在一些危险;见Law of Demeter

但是,如果您希望这样做,只需使用您希望拥有的子对象创建每个对象。

对于您的示例,您将创建以下内容:

public class House
{
    private readonly List<Room> _rooms = new List<Room>();

    public Room Rooms(string roomName)
    {
        return _rooms.Single(x => x.Name == roomName);
    }

    public void AddRoom(string roomName)
    {
        _rooms.Add(new Room { Name = roomName });
    }
}

public class Room
{
    private readonly List<Door> _doors = new List<Door>();

    public string Name { get; set; }

    public Door Doors(string doorName)
    {
        return _doors.Single(x => x.Name == doorName);
    }

    public void AddDoor(string doorName)
    {
        _doors.Add(new Door { Name = doorName });
    }
}

public class Door
{
    private readonly List<Window> _windows = new List<Window>();

    public string Name { get; set; }

    public Window Windows(string windowName)
    {
        return _windows.Single(x => x.Name == windowName);
    }

    public void PlaceWindow(string windowName)
    {
        _windows .Add(new Window { Name = windowName }); 
    }
}

public class Window
{
    public string Name { get; set; }
}

答案 1 :(得分:0)

这是一种更容易构建多层对象的替代模块化方法。

public abstract class NamedObject
{
    public string Name { get; set; }
}

public class ComponentCollection<T> where T : NamedObject
{
    private readonly List<T> _components = new List<T>();

    public T this[string name]
    {
        get { return Get(name); }
    }

    public T Get(string name)
    {
        return _components.Single(x => x.Name == name);
    }

    public void Add(T component)
    {
        _components.Add(component);
    }

    public void Remove(string name)
    {
        _components.RemoveAll(x => x.Name == name);
    }
}

这使组件更易于创建和维护:

public class House
{
    public readonly ComponentCollection<Room> Rooms = new ComponentCollection<Room>();
}

public class Room : NamedObject
{
    public readonly ComponentCollection<Door> Doors = new ComponentCollection<Door>();
}

public class Door : NamedObject
{
    public readonly ComponentCollection<Window> Windows = new ComponentCollection<Window>();
}

public class Window : NamedObject
{
}

调用语法的变化相当小:

[TestMethod]
public void House_AddFrostedPaneWindowToLivingRoomFrontDoor_WindowExists()
{
    var myHouse = new House();
    myHouse.Rooms.Add(new Room { Name = "Living Room" });
    myHouse.Rooms["Living Room"].Doors.Add(new Door { Name = "Front" });
    myHouse.Rooms["Living Room"].Doors["Front"].Windows.Add(new Window { Name = "Frosted Four Pane" });

    var window = myHouse.Rooms["Living Room"].Doors["Front"].Windows["Frosted Four Pane"];

    Assert.IsNotNull(window);
}