如何创建一个创建类的多个对象的循环? [英] How do I create a loop that creates multiple objects of a class?

查看:80
本文介绍了如何创建一个创建类的多个对象的循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这有可能吗?)所以(例如)我想要一个名为Fruit的课程......

 class Fruit 
{
private string _name;
public string Name {get {return _name; }
}

我想制作一个循环,创造了一堆这些所谓的"水果"。像这样(我知道这不会像这样工作但是)...

 for(int i = o; i< 100; i ++)
{
Fruit Apple< i> =新水果();
}

我希望这些对象的名称会像"Apple0","Apple1"一样出现。 ,"Apple2"......


所以。 这甚至可以用c#或ANY编程语言(当然是以不同的方式)吗?


谢谢。

解决方案

那么,&NBSP;水果对象必须加载到水果对象数组或水果对象集合中,这些对象是一组可访问的相关对象。否则,是的, 什么&NBSP;是在一个循环制作对象的点?  


https://www.c-sharpcorner.com/uploadfile/Ravish001/how-to-declare-an-array-of-custom-type/


https://dzone.com/articles/different-ways-of-creating-list-of-objects-in-c


收集访问....


foreach(学生中的var学生)


{


  &NBSP;&NBSP; var name = student.Name;


 }


读或写....


  int < span style =""> i
= 0 ; i
< 学生。 count() ; i ++)

{

   
var name = students [i] .Name;


  &NBSP;&NBSP;学生[i] .Name =" help" }





(Is this even possible?) So (for example) I wanted a class called Fruit...

class Fruit
{
    private string _name;
    public string Name { get { return _name; } }
}

and I wanted to make a loop that created a bunch of these said "Fruits" like this (I know this wouldn't work like this but)...

for (int i = o; i < 100; i++)
{
    Fruit Apple<i> = new Fruit();
}

and I would hope that the names of these objects would come out like "Apple0", "Apple1", "Apple2"...

So.  Is this even possible to do (in a different way of course) in c# or ANY programming language?

Thanks.

解决方案

Well,  the fruit object would have to be loaded into an array of fruit objects or a collection of fruit objects, a group of related objects that are accessible. Becuase otherwise,  what  is the point of being in a loop making objects?  

https://www.c-sharpcorner.com/uploadfile/Ravish001/how-to-declare-an-array-of-custom-type/

https://dzone.com/articles/different-ways-of-creating-list-of-objects-in-c

collection access....

foreach(var student in students)

{

     var name = student.Name;

 }

read or write....

 for (int i = 0; i < students.count(); i++)
{
   
var name = students[i].Name;

     students[i].Name = "help"}



这篇关于如何创建一个创建类的多个对象的循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆