如何在Java中为未知数量的参数编写构造函数?

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

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 &lt;Student&gt;listofstudents=new ArrayList&lt;&gt;();
    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 Courses. (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 &lt;Student&gt;listofstudents=new ArrayList&lt;&gt;();
    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(&quot;Name&quot;);
new Course(&quot;Name&quot;, new Student());
new Course(&quot;Name&quot;, new Student(), new Student(), new Student());

huangapple
  • 本文由 发表于 2020年8月15日 04:12:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63419468.html
匿名

发表评论

匿名网友

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

确定