Adding infinite numbers for objects in a class (in theory)

huangapple go评论69阅读模式
英文:

Adding infinite numbers for objects in a class (in theory)

问题

I understand your question. You can achieve this by using a List to store passenger objects. Here's the modified code:

class BusSimulator
{
    List<Passenger> passengers = new List<Passenger>();

    public void AddPassenger()
    {
        if (passengers.Count < 25) // Maximum of 25 passengers
        {
            Passenger newPassenger = new Passenger();
            newPassenger.Name = Console.ReadLine();
            newPassenger.Age = int.Parse(Console.ReadLine());
            newPassenger.Destination = int.Parse(Console.ReadLine());
            passengers.Add(newPassenger);
        }
        else
        {
            Console.WriteLine("The bus is already full with 25 passengers.");
        }
    }
}

class Passenger
{
    public string Name { get; set; }
    public int Age { get; set; }
    public int Destination { get; set; }
}

With this code, each time you call AddPassenger(), it will create a new Passenger object and add it to the list.

英文:

I am on my last project for my programming course, we are making a bus simulator where the passengers are gonna be made as objects.

I have managed to make the class and everything, but I am wondering if there is some way to make an infite number of objects instead. Instead of doing this down below i want it to make a new object everytime that option is picked. (We are making a menu for different methods, this one is for adding passengers)

 public void add_Passenger()
    {
        passenger passanger1 = new passenger();
        passanger1.name = Console.ReadLine();
        passanger1.age = int.Parse(Console.ReadLine());
        passanger1.destination = int.Parse(Console.ReadLine());
        
    }

here is also the class itself if its needed

class passenger
{
    public string name;
    public int age;
    public int destination;
}

** edit **
ok not infinite, but i ment more that i want the program to make a new object whenever that option is picked instead of me having to make
passenger1
passenger2
passenger3

and so on. The bus is supposed to have a maximum of 25 passangers.
Sorry for my vague explanation im new to programming and my english is not super good

答案1

得分: 1

如果我理解正确,听起来你想将passenger对象存储在超出该方法范围的更高作用域。你在这个旅程上的第一站是一个类级成员。

首先,让我们修复你的类以使用C#约定(大写字母,使用属性而不是字段):

class Passenger
{
    public string Name { get; set; }
    public int Age { get; set; }
    public int Destination { get; set; }
}

现在,在具有该方法的类中,添加一个字段来保存你的 "passengers"。例如:

class YourClass
{
    private IList<Passenger> passengers = new List<Passenger>();

    public void AddPassenger()
    {
        var passenger = new Passenger();
        passenger.Name = Console.ReadLine();
        passenger.Age = int.Parse(Console.ReadLine());
        passenger.Destination = int.Parse(Console.ReadLine());
        passengers.Add(passenger);
    }

    // 该类的其余部分...
}

你会注意到我也更新了一些名称,只是为了遵循约定。还有更多可以改进的地方,比如输入的错误处理,使用特定的输入提示,将输入的读取与使用输入的逻辑分开等等。但是出于学术目的,我们从小开始。

现在,你的类实例在类级别保持着一个“乘客”列表,每次调用该方法时,它都会将另一个“乘客”添加到这个持续的列表中。

如果其他代码需要访问这个列表,你可以将私有字段更新为公共属性。或者你可以继续将其存储范围提升到超出类的范围,比如数据库或文件。你下一步要做什么取决于正在构建的系统的需求。但是总体来说,如果你只想在方法结束后保留该对象,你只需要将它保存到方法范围之外的某个地方。

英文:

If I understand correctly, it sounds like you want to store your passenger objects in a higher scope that persists outside of that method. Your first stop on that journey is a class-level member.

First let's fix your class to use C# conventions (capital letter, properties instead of fields):

class Passenger
{
    public string Name { get; set; }
    public int Age { get; set; }
    public int Destination { get; set; }
}

Now in the class with the method, add a field to hold your "passengers". For example:

class YourClass
{
    private IList&lt;Passenger&gt; passengers = new List&lt;Passenger&gt;();

    public void AddPassenger()
    {
        var passenger = new Passenger();
        passenger.Name = Console.ReadLine();
        passenger.Age = int.Parse(Console.ReadLine());
        passenger.Destination = int.Parse(Console.ReadLine());
        passengers.Add(passenger);
    }

    // the rest of the class...
}

You'll notice I updated some names there as well, just to stick with convention. There are several more improvements which can be made, such as error handling for input, using specific prompts for input, separating the reading of input from the logic that uses the input, etc., etc. But we're starting small for academic purposes.

Now your class instance is keeping a list of "passengers" at the class level, and each time that method is called it will add another "passenger" to that ongoing list.

You can update the private field to be a public property if other code needs to access this list. Or you can continue to lift the scope of where it's stored into something that persists outside of the class, such as a database or file somewhere. Where you go from here really depends on the needs of the system being built. But overall if you just want to keep that object after the method ends, you just need to save it to something outside the scope of the method.

huangapple
  • 本文由 发表于 2023年5月13日 21:44:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定