C# 在C中创建用户指定的n个对象#

C# 在C中创建用户指定的n个对象#,c#,variables,object,C#,Variables,Object,我有一个电梯程序,用户在其中输入要创建的人数 一旦人数增加,他们需要一个接一个地请求电梯 person对象类的代码为: class Person { public int currentFloor; public int destinationFloor; public bool requestMade; // user does not request until inside the lift public bool isWaiting; // ma

我有一个电梯程序,用户在其中输入要创建的人数

一旦人数增加,他们需要一个接一个地请求电梯

person对象类的代码为:

class Person
{
    public int currentFloor;
    public int destinationFloor;
    public bool requestMade; // user does not request until inside the lift
    public bool isWaiting;

    // manual
    public Person(int currentFloor, int destinationFloor)
    {
        this.currentFloor = currentFloor;
        this.destinationFloor = destinationFloor;
    }

    // automatic
    public Person()
    {
        var r = new Random();
        currentFloor = r.Next(0, 4); // assign random floor as current (5 lifts)
        assignRandomDestination(); // assign random floor for destination but cannot be the same as current, else randomise destination again
    }

    private void assignRandomDestination()
    {
        var r = new Random();
        destinationFloor = r.Next(0, 4);

        if (destinationFloor == currentFloor)
        {
            assignRandomDestination();
        }
    }

    public void state(Person person, bool waiting)
    {
        if (waiting == true) { person.isWaiting = true; } else { person.isWaiting = false; }
    }

    public bool request()
    {
        return requestMade;
    }
}

我怎样才能单独调用它们呢?

您应该为电梯创建另一个类,并添加System.Collections.Generic

  List<Person> passenger = new List<Person>();

  //Make an Instance of your Person.
  Person my1stperson = new Person();

  //Access their member by dot operator.
  my1stperson.currentFloor = 13;

  //After setting their fields; You can add them to the list by.
  passenger.Add(my1stperson);

  // if you have an Elevator class with currentLocation field on the elevator.

  Elevator.currentLocation = passenger[0].currentFloor; //<- only works if the field is static. Or use the Index. LinQ also works for list. Enumerables etc.

  //If not static, Instantiate the Elevator Class.
  Elevator myElevator = new Elevator();
List乘客=新列表();
//以你的个人为例。
Person my1stperson=新的Person();
//通过点运算符访问其成员。
my1stperson.currentFloor=13;
//在设置了它们的字段之后;您可以通过将它们添加到列表中。
乘客。添加(我的第一个乘客);
//如果您有一个电梯类,电梯上有currentLocation字段。

电梯。当前位置=乘客[0]。当前楼层//创建一个
列表
,并使用一个循环让用户添加到itI中,这样就可以保存一个集合中的人员集合,例如列表。顺便说一句:只是一个简短的评论:公共无效状态(Person-Person,bool-waiting)很奇怪。如果你想知道一个“人”的“状态”,那么你不需要“告诉这个人他们是谁”——也就是说,把这个人作为一个参数传递进去。这似乎是一个很好的代码片段,可以用来进行分析,获取一些指针,是的,你是对的。要改变这一点,我怎么称呼他们?