繁体   English   中英

如何正确地从另一个类调用构造函数,然后将其打印为数组以进行控制台?

[英]How to call constructor from another class correctly and then print it as an array to console?

我试图通过从另一个班级调用一个构造函数并在我的主班级中初始化一个数组,将5组学生的姓名和分数打印到控制台上

当前代码显示了我尝试调用构造函数并初始化数组的方式

学生班:

     package studentinfo;

public class Student {

String name;    
int score;    

public Student (String sname, int sscore) {
sname= name; 
sscore = score; 

}
    public void greeting() {
        //Student 1 
        Student student1 = new Student ("Mike" , 40);
        System.out.println("My name is: " + student1.name + " " +"My score is:" + student1.score);
        //Student 2
        Student student2 = new Student ("Tom" , 50);
        System.out.println("My name is: " + student2.name + " " +"My score is:" + student2.score);
        //Student 3
        Student student3 = new Student ("John" , 60);
        System.out.println ("My name is: " + student3.name +" " +",My score is:" +student3.score); 
        //Student 4
        Student student4 = new Student ("May" , 80);
        System.out.println ("My name is: " + student4.name +" " +",My score is:" +student4.score); 
        //Student 5 
        Student student5 = new Student ("Lucy" , 50);
        System.out.println ("My name is: " + student5.name +" " +",My score is:" +student5.score); 
    }
}

学生信息课:

    **package studentinfo;
import java.util.Arrays;

public class StudentInfo {

    static Student[] students;
    static int maxStudentNumber; 

    public static void main(String[] args) {

        addStudents();

        int maxStudentNumber = 5;

        printStudents();

        System.out.println ("The maximum amount of students per class is: " + maxStudentNumber +"" );

    }

    static void addStudents() {
    Student students = new Student(" " , 5);

    }
    static void printStudents() {
    students[1].greeting();
    students[2].greeting();
    students[3].greeting();
    students[4].greeting();
    students[5].greeting();

    System.out.println (Arrays.toString(students));         
    }
}**

您的学生班级应该是这样的:

public class Student {

    String name;
    int score;

    public Student (String sname, int sscore) {
        name = sname;
        score = sscore;

    }
    public void greeting() {
        System.out.println("My name is: " + this.name + " " +"My score is:" + this.score);
    }
}

当您调用greeting()时 ,只有调用它的学生才能打招呼。 并非所有学生

您可以将StudentInfo类更改为此:

public class StudentInfo {
    static Student[] students;
    static int maxStudentNumber;

    public static void main(String[] args) {

        maxStudentNumber = 5;
        addStudents();

        printStudents();

        System.out.println ("The maximum amount of students per class is: " + maxStudentNumber +"" );
    }

    static void addStudents() {

        students = new Student[maxStudentNumber];
        students[0] = new Student ("Mike" , 40);
        students[1] = new Student ("Tom" , 50);
        students[2] = new Student ("John" , 60);
        students[3] = new Student ("May" , 80);
        students[4] = new Student ("Lucy" , 50);
    }
    static void printStudents() {
        for(int i=0;i<maxStudentNumber;i++) {
            students[i].greeting();
        }
    }
}

希望它对您有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM