英文:
How do I write a constructor for unknown number of parameters in java?
问题
我基本上为每门大学课程创建一个类,在这门课程中,任意数量的学生都可以申请。我想知道如何创建一个构造函数,无论我有多少个“Student”对象,都可以是1个、15个或更多... 因为我知道有一种方法,只是想不起来了...
import java.util.ArrayList;
public class Course {
String name;
ArrayList<Student> listofstudents = new ArrayList<>();
int noofStudents;
//类“Student”已定义。
Course(String name, Student... students) {
this.name = name;
for (Student student : students) {
listofstudents.add(student);
}
}
}
英文:
I'm basically making a class for each university course where any number of students can apply. I wanted to know how can I have a constructor where it doesn't matter if my number of 'Student' objects is 1 or 15 or more...cause I know there's a way, just can't remember it...
import java.util.ArrayList;
public class Course {
String name;
ArrayList <Student>listofstudents=new ArrayList<>();
int noofStudents;
//The class Student is defined.
Course(String name,Student s1, Student s2){
this.name=name;
listofstudents.add(s1);
listofstudents.add(s2);
//Do I have to do this for every possible number of student?
}
}
答案1
得分: 3
正如你的另一个回答所指出的,构造函数可以像常规方法一样拥有可变参数。然而,你应该考虑是否要这样做。也就是说,注册在特定课程中的学生是次要属性,你可以使一个Course
处于有效状态而不需要它们。因此,考虑不要通过构造函数接受学生,而是通过一个或多个方法将学生添加到现有的Course
中(你可能无论如何都需要这个方法)。然后,在初始化Course
之后,通过该方法将学生添加到课程中。
void addStudent(Student s) {
listofstudents.add(s);
}
void addStudents(Student ... s) {
if (s != null) {
listofstudents.addAll(java.util.Arrays.asList(s));
}
}
英文:
As your other answer observes, constructors can have variable arguments just like regular methods can have. However, you should consider just saying no. That is, the students enrolled in a given course are a secondary attribute, and you can put a Course
into a valid state without them. Consider, then, not accepting the students via the constructor, but instead having one or more methods to add students to existing Course
s. (You might need that anyway.) Then just add the students via that route after initializing the Course
.
void addStudent(Student s) {
listofstudents.add(s);
}
void addStudents(Student ... s) {
if (s != null) {
listofstudents.addAll(java.util.Arrays.asList(s));
}
}
答案2
得分: 1
可以使用可变参数(varargs)。可变参数是一种允许你创建具有任意数量参数的方法的技术。
import java.util.ArrayList;
public class Course {
String name;
ArrayList<Student> listofstudents = new ArrayList<>();
int noofStudents;
// 学生类已定义。
Course(String name, Student... s){
Student[] stds = s;
this.name = name;
// 你的代码
}
}
使用可变参数很简单。但是需要遵循一些规则:
- 每个方法只能有一个可变参数
- 可变参数必须是最后一个参数
每次使用可变参数时,Java 编译器都会创建一个数组来保存给定的参数。
现在你可以按以下方式使用构造函数:
new Course("Name");
new Course("Name", new Student());
new Course("Name", new Student(), new Student(), new Student());
英文:
You can use varargs. Variable Arguments is a technology that allows you to create methods with an arbitrary number of arguments.
import java.util.ArrayList;
public class Course {
String name;
ArrayList <Student>listofstudents=new ArrayList<>();
int noofStudents;
//The class Student is defined.
Course(String name,Student ... s){
Student [] stds = s;
this.name=name;
//your code
}
}
Varargs are straightforward to use. But there're a few rules we have to keep in mind:
- Each method can only have one varargs parameter
- The varargs argument must be the last parameter
Every time we use varargs, the Java compiler creates an array to hold the given parameters.
Now you can use the constructor in this way:
new Course("Name");
new Course("Name", new Student());
new Course("Name", new Student(), new Student(), new Student());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论