【问题标题】:associations c#关联 c#
【发布时间】:2013-04-10 14:07:56
【问题描述】:

我试图在 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”部分。

请帮忙!

【问题讨论】:

  • house 94 = new house();?这甚至可以编译吗?
  • 可能不相关,但变量名不能以数字开头 - 将“94”更改为“house94”或任何有意义的内容
  • House 没有 addRoom() 方法。
  • 这段代码有很多问题,我不知道从哪里开始......不过我会尝试回答。
  • doesnt seem to like the 'addRoom' part。您是否希望编译器自己为这个函数创建实现,只是从名称中推断出逻辑?

标签: c# class associations diagram


【解决方案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;
            }
        }
    }
 }

【讨论】:

    【解决方案2】:

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

    // 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 类的构造函数占用一个房间,因此在创建房屋时,从一开始就添加一个房间,例如House94 = new House(new Room());。您可以拨打House94.Rooms.Add(new Room())添加更多房间。

    【讨论】:

    • 我不认为将显然是编程新手的人与Properties 混淆是一个好主意。只是我的两分钱。尽管如此,+1 的答案。
    • 是的,我实际上也在考虑这个问题,但话又说回来,属性是 C# 的重要组成部分。
    【解决方案3】:

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

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

    【讨论】:

    • 所有代码都可以接受,直到行 'house.addRoom(bedroom);'它不会接受代码的“addRoom”部分
    • @Tom 那是因为方法addRoom 没有定义。至少,我们不能通过查看您的代码来判断。
    猜你喜欢
    • 2010-12-27
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 2014-05-19
    • 2015-01-10
    • 2017-12-04
    相关资源
    最近更新 更多