我有一个Main.java文件,在这个类中我创建了一个学生对象的ArrayList。我想将这些对象传递给另一个类,在另一个名为IntroSentenceHigh的包中。
以下是我的Main.java文件中的代码:
public class Main
{
public static void main(String[] args)
{
ArrayList<Student> StudentList = new ArrayList<Student>();
// Student details --- Name, Year Level, Gender, Average Test Score, Exam Score, Classwork, Homework, Practical Work, Behaviour
Student student1 = new Student("Bob", 12, "M", 100, 100, 1, 1, 1, 1);
Student student1 = new Student("John", 12, "M", 100, 100, 1, 1, 1, 1);
StudentList.add(student1);
StudentList.add(student2);
// Loop through all student data in the array
for (int i = 0; i < StudentList.size(); i++)
{
System.out.println(StudentList.get(i).viewStudentInfo());
}
IntroSentenceHigh introSentenceHigh = new IntroSentenceHigh();
我有一个Student.java类,其中包含以下内容:
public class Student
{
private String name;
private int yearLevel;
private String gender;
// Student marks
private int testAverage;
private int exam;
// Other student data
private int classwork;
private int homework;
private int practicalWork;
private int behaviour;
public Student(String name, int yearLevel, String gender, int testAverage, int exam, int classwork,
int homework, int practicalWork, int behaviour)
{
this.name = name;
this.yearLevel = yearLevel;
this.gender = gender;
this.testAverage = testAverage;
this.exam = exam;
// Student's are given scores from 1 to 3.
// 1 is best, 3 is worst
this.classwork = classwork;
this.homework = homework;
this.practicalWork = practicalWork;
this.behaviour = behaviour;
}
public String getName()
{
return name;
}
public int getYearLevel()
{
return yearLevel;
}
public String getGender()
{
return gender;
}
public int getTestAverage()
{
return testAverage;
}
public int getExam()
{
return exam;
}
public int getClasswork()
{
return classwork;
}
public int getHomework()
{
return homework;
}
public int getPracticalWork()
{
return practicalWork;
}
public int getBehaviour()
{
return behaviour;
}
public String viewStudentInfo()
{
String studentInfo;
studentInfo = "Student name: " + getName() + "\n" +
"Year: " + getYearLevel() + "\n" +
"Gender: " + getGender() + "\n" +
"Test Average: " + getTestAverage() + "\n" +
"Exam result: " + getExam()+
"\n";
return studentInfo;
}
}
最后,我有一个带有以下代码的IntroSentenceHigh.java类:
public class IntroSentenceHigh
{
private String introSentenceHigh1 = "Student statement 1";
private String introSentenceHigh2 = "Student statement 2";
public String getIntroSentenceHigh1()
{
return introSentenceHigh1;
}
public String getIntroSentenceHigh2()
{
return introSentenceHigh2;
}
基本上,我想在IntroSentenceHigh类中访问学生姓名和性别,但我不知道如何从我创建ArrayList的Main.java类中获取它。我在同一个主题上寻找答案,但作为一个初学者,我不知道如何修改我的代码以使其工作。
任何帮助都会很棒。谢谢!
答案 0 :(得分:1)
有很多方法可以完全按照自己的意愿行事,最简单的方法就是使用参数化构造函数。这意味着将一个ArrayList参数添加到您的IntroSentenceHigh类(顺便说一下,这不是一个非常直观的名称),并在该类中设置一个等于ArrayList的全局变量。这是一种方法:
public Class IntroSentenceHigh {
private ArrayList<Student> studentList;
public IntroSentenceHigh(ArrayList<Student> studentList) {
this.studentList = studentList
}
现在,您的全局变量studentList包含您在Main中创建的学生的ArrayList。然后,您可以使用在Student类中声明的getter来接收所需的信息。一个例子是:
String aName = studentList.get(0).getName();
请记住在创建IntroSentenceHigh实例时添加ArrayList参数(幸运的是,编译器也会提醒您该要求;)
答案 1 :(得分:0)
使用参数向IntroSentenceHigh
添加构造函数:ArrayList
并创建IntroSentenceHigh
的实例,如下所示new IntroSentenceHigh(StudentList )