如何使用Java中的数组参数初始化对象

时间:2015-06-07 16:08:20

标签: java arrays object

我正在尝试编写一个代码,其中类的对象在其参数中有一个数组。我编写了参数化构造函数,如下所示 -

public abstract class StudentHS {

private String firstName;
private String lastName;
private String rollNumber;
public int[] marksAcquired;
public int hundreds;

// constructor
public StudentHS (String first, String last, String roll, int[] marks) {

    firstName = first;
    lastName = last;
    rollNumber = roll;
    marksAcquired = marks;

}

现在,当我尝试在对象数组中初始化此类的对象时,我收到一条错误,指出构造函数未定义。

public class ResultSystemTest {

public static void main(String[] args) {
    StudentHS studentArts[] = new StudentHS[3];
    StudentHS studentCommerce[] = new StudentHS[3];
    StudentHS studentScience[] = new StudentHS[3];

    studentArts[ 0 ] = new StudentArts("Priyanka", "Ray", "01", {56, 59, 61, 72, 65, 63, 58});
}

StudentArts,StudentCommerce和StudentScience是StudentHS的子类。

我在哪里错了?

1 个答案:

答案 0 :(得分:1)

你必须在传递类似

时定义它的类型
 studentArts[ 0 ] = new StudentArts("Priyanka", "Ray", "01", new int[]{56, 59, 61, 72, 65, 63, 58});