繁体   English   中英

如何向类添加类对象列表?

[英]How to add a list of class objects to a class?

我有3个班级:房屋,公寓和居民区。 每个类都具有基本属性,例如字符串名称。 问题是如何在它们之间创建链接,如下所示:

1.房屋类别应具有Apartment属性(公寓作为列表(?),因为一所房屋可以拥有多个公寓)

2.具有财产房屋(单身)(公寓所在的房屋)的公寓房。 还有居民(住在公寓的人)的收藏(清单?)。

3.具有公寓(单身)财产的居民阶层。

我已经使用继承创建了这些类的结构,但是它不起作用-无法在未指定Apartment的情况下创建House,无法在没有House的情况下添加Apartment等。

public class House
{
    public int number { get; set; }
    public string street { get; set; }
    public List<Apartment> apartments = new List<Apartment>();
}
public class Apartment:House
{
    public int apt_number { get; set; }
    public int apt_Rooms { get; set; }
    public House house;
    public List<Resident> residents = new List<Resident>();
}
public class Resident
{   public string name {get;set;}
    public string surname {get;set}
    public Apartment apartment {get;set}
}

如评论中所述,(似乎)使Apartment继承房屋毫无用处。 如果删除继承关系,则可以参考House对象创建一些Apartment对象,并将其添加到house的Apartment列表中。

House house = new House();
Apartment app1 = new Apartment { house = house };
Apartment app2 = new Apartment { house = house };
Apartment app3 = new Apartment { house = house };

house.apartments = new List<Apartment>{ app1, app2, app3 };

宽容与组成不同。 当您从A继承B时,您说“ B A ”。 在您的情况下,当您从House继承Apartment时,您会说“ Apartment就是House ”。 这显然是不正确的,因为公寓不是房屋,所以它是房屋的一部分 所以Apartment不应继承House

作为组成部分的关系由组成表示。 您已通过向House添加公寓列表(并将House属性添加到Apartment )来完成此操作。

但是,您通常希望保持父级列表与子级父级属性之间的完整性。 例如,当您向House添加一个Apartment时,您需要相应地设置Apartment.House

使用List<T>无法实现此行为。 实际上, List<T>并不是公开为这种公共属性,而是用于内部数据存储。 为了您的目的,您需要一个System.Collections.ObjectModel.Collection<T> ,它允许您进行所需的自定义。

这就是如何处理HouseApartment之间的关系。 您需要对Apartment and Resident做同样的事情:

public class House
{
    public int Number { get; set; }
    public string Street { get; set; }
    public ApartmentCollection Apartments { get; private set; }

    public House() {
        Apartments = new ApartmentCollection(this);
    }
}

public class Apartment
{
    private House house;

    public int AptNumber { get; set; }
    public int AptRooms { get; set; }
    public House House {
        get {
            return house;
        }
        set {
            house?.Apartments.Remove(this);
            house = value;
            house?.Apartments.Add(this);
        }
    }
    public List<Resident> Residents = new List<Resident>();
}

public class ApartmentCollection : Collection<Apartment> {
    private readonly House parent;

    public ApartmentCollection(House parent) {
        this.parent = parent;
    }

    protected override void InsertItem(int index, Apartment item) {
        if (item == null) {
            throw new ArgumentNullException(nameof(item));
        }
        if (Contains(item)) {
            return;
        }

        base.InsertItem(index, item);
        item.House = parent;
    }

    protected override void SetItem(int index, Apartment item) {
        if (item == null) {
            throw new ArgumentNullException(nameof(item));
        }

        Apartment oldItem = this[index];
        if (oldItem.Equals(item)) {
            return;
        }

        int oldIndexOfItem = IndexOf(item);
        base.SetItem(index, item);
        oldItem.House = null;
        item.House = parent;

        //If the item was in the collection before, remove it from its old position
        if (oldIndexOfItem >= 0) {
            base.RemoveItem(oldIndexOfItem);
        }
    }

    protected override void RemoveItem(int index) {
        Apartment removedItem = this[index];
        base.RemoveItem(index);
        removedItem.House = null;
    }

    protected override void ClearItems() {
        Apartment[] removedItems = new Apartment[Count];
        CopyTo(removedItems, 0);
        base.ClearItems();
        foreach(Apartment removedItem in removedItems) {
            removedItem.House = null;
        }
    }
}

现在,您既可以设置Apartment.House也可以从House.Apartments添加和删​​除项目。 另一端将始终自动更新。 此外, ApartmentCollection将防止两次添加同一公寓或向公寓集合添加null值。 List<T>都不做。

Sefe对您的课程提出了一个很好的问题。

您不应该让Apartment从House继承。

如果让我们说一个带有属性的类“水果”,则此概念更有意义。 然后,您会从“水果”继承一些特定的类,例如“苹果”或“梨”。 它们都具有水果的相同特性,并且也具有自己的特性。

要解决您的特定问题:您忘记了一些“;” 我删除了继承。 另外,您还没有为列表或Apartment内的属性房屋指定获取或设置。 要创建类的变量,请参见上面Aars93中的文章。

public class House
{
    public int number { get; set; }
    public string street { get; set; }
    public List<Apartment> apartments = new List<Apartment>();
}

public class Apartment
{
    public int apt_number { get; set; }
    public int apt_Rooms { get; set; }
    public House house;
    public List<Resident> residents = new List<Resident>();
}

public class Resident
{   public string name {get;set;}
    public string surname {get;set;}
    public Apartment apartment {get;set;}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM