创建类的多个实例 [英] Creating Multiple Instances of a Class

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

问题描述

您好,对于作业,我需要创建一个学生成绩簿,要求用户输入他们的学生人数,我需要创建实例(student1,student2,student3 ....)对于每个学生,然后为每个
分配属性。我尝试使用for循环,但它没有用。请告诉我如何为班上的每个学生创建一个实例,我可以自己完成其余的代码。提前致谢....

Hello, for an assignment I need to create a student mark book where the user is asked to input the number of students they have, and I need to create instances (student1, student2, student3....) for each of the students, then assign properties to each of them. I tried using a for loop, but it did not work. Please just show me how I can create an instance for each student in the class, I can complete the rest of the code myself. Thanks in advance....

PS:我的老师已经允许其他来源,特别是MSDN论坛寻求帮助......

P.S: My teacher has given permission to ask other sources, especially MSDN forums for help...

推荐答案

简单的答案是这样的......

The simple answer is something like so...

Student student1 = new Student();
student1.Name = "Joe Bloggs";
student1.ID = 123;

Student student2 = new Student();
student2.Name = "Fred Nurk";
student2.ID = 456;

但我怀疑这不是你想要的,因为如果学生人数变数你就不会有问题称他们为student1,student2,student3等,而事先并不知道有多少人。所以看起来你需要一个数组
或某种类型的列表,你可以根据需要填写尽可能多的学生。事实上,如果正确完成,'for'循环就会达到这个目的。

But I suspect that's not what you are after, because there is a problem if the number of students is variable you can't call them student1, student2, student3 and so on without knowing in advance how many there are. So it looks like you will need an array or list of some kind, that you can fill out with as many students as needed. A 'for' loop would, in fact, serve this purpose if done correctly.

我们不应该为你写太多代码,但如果你告诉我们你有什么到目前为止(没有用)我们可能会指出你出错的地方。

We are not supposed to write too much of your code for you, but if you show us what you have so far (that didn't work) we might be able to point out where you went wrong.


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

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