-6

我试图在 c# 中的两个类之间建立关联。该关联是从一个班级(教室)到另一个班级(教室)的一个末端和 1 .. * 的一个本质。换句话说,来自教室的数据可以在教室 1 中使用多次,但至少需要使用一次。

例如,一个房间只能有一个房子,但一个房子必须至少有一个房间或多个房间。

到目前为止我有这个,但它似乎没有创建一个对象的实例..

 class house
 {
    string name;
    int house_num;

    list<room> rooms;

    public house()
    {
      rooms = new list<room>();
      rooms.Add(new room());
    }
 }

 class room
 {
   int num_of_rooms;

 }

这就是我在主cs文件中调用的内容..

 house 94 = new house();
 room bedroom = new room();

 house.addRoom(bedroom);

所以我基本上想说一所房子必须至少有一个房间,但它也可以有你能想到的尽可能多的房间。出于某种原因,上面似乎不喜欢代码的“addRoom”部分。

请帮忙!

4

3 回答 3

1

我认为这基本上是您需要的以及您可以扩展的内容:

// Define a class for a house
public class House
{
    // Constructor for the House class
    // This gets called whenever your code creates a "new House(...);"
    public House(Room room)
    {
        // Initialize the list of rooms.
        _rooms = new List<Room>;
        // Add the passed room to the list of rooms
        _rooms.Add(room);
    }

    // This a "backing field" for the property below it. It is a
    // private variable that stores information about the class.
    private List<Room> _rooms;
    // This is the property that exposes the above backing field.
    // This property only has a "get" method, which means that you can only
    // read the property value. However, you can still add rooms to the list,
    // even though that may seem like writing. The difference is that you cannot
    // assign a completely new list to the property.
    public List<Room> Rooms
    {
        get { return _rooms; }
    }

    // You can add methods to a class to perform a task.
    // The method below adds a room to the list of rooms.
    public void AddRoom(Room room)
    {
        _rooms.Add(room);
    }
}

public class Room
{

}

该类的构造函数House占用一个房间,因此在创建 House 时,从一开始就添加一个房间,例如House94 = new House(new Room());. 您可以通过调用添加更多房间House94.Rooms.Add(new Room())

于 2013-04-10T14:17:25.217 回答
1

您在这里有一些很好的示例,但这里有一个功能齐全的控制台示例,可帮助指导 C# 新手。这不是福音或最佳实践,因为我试图不让初学者感到困惑,但我已经包含了大多数事情的简单示例(只读字段意味着只能由构造函数设置,简单的属性等)但我希望是一个好的初学者!

namespace Example.ActualApp
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// The Program
    /// </summary>
    internal class Program
    {
        /// <summary>
        /// The main entry point.
        /// </summary>
        static void Main(string[] args)
        {
            // Create a new house with a bedroom
            Room bedroom1 = new Room("Master bedroom");
            House sampleHouse = new House("My house", bedroom1);

            // Add a room
            Room sittingRoom = new Room("Main sitting room");
            sampleHouse.AddRoom(sittingRoom);

            // Wait for user to press a button
            Console.WriteLine("\r\nFinished, press a key to end.");
            Console.ReadKey();
            return;
        }
    }

    /// <summary>
    /// House class
    /// </summary>
    internal class House
    {
        // Private fields
        private readonly string _name = string.Empty;
        private List<Room> _rooms = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="House"/> class.
        /// </summary>
        /// <param name="name">The name of the house.</param>
        /// <param name="room">The default room.</param>
        public House(string name, Room room)
        {
            // Store the house name and initialise the rooms collection
            _name = name;
            _rooms = new List<Room>();
            Console.WriteLine("New house created called '{0}'", name);

            // Now add the default room
            this.AddRoom(room);
        }

        /// <summary>
        /// Adds a room to the house.
        /// </summary>
        /// <param name="room">The room.</param>
        public void AddRoom(Room room)
        {
            _rooms.Add(room);
            Console.WriteLine("Room called '{0}' added to house called '{1}'", room.Name, _name);
        }
    }

    /// <summary>
    /// Room class
    /// </summary>
    internal class Room
    {
        private string _roomName = string.Empty;

        /// <summary>
        /// Initializes a new instance of the <see cref="Room"/> class.
        /// </summary>
        /// <param name="name">The name of the room.</param>
        public Room (string name)
        {
            _roomName = name;
            Console.WriteLine("New room created called '{0}'", name);
        }

        /// <summary>
        /// Example property (never pass fields around)
        /// </summary>
        public string Name
        {
            get
            {
                return _roomName;
            }

            set
            {
                _roomName = value;
            }
        }
    }
 }
于 2013-04-10T14:40:22.820 回答
0

回顾代码的其他问题,您可以在 class 的构造函数中添加一个或更多房间house,这将保证房子至少有您在那里指定的房间。

编辑:看来你已经这样做了,我什至不确定你在问什么:p

于 2013-04-10T14:10:52.787 回答