我已将其作为任务的一部分:
private static void addStudent(int id, String firstName, String lastName,int
mathsMark1, int mathsMark2, int mathsMark3, int englishMark1, int
englishMark2,
int englishMark3)
并附上说明:
创建一个新的Student对象和两个AsignmentMarks对象(用于数学和英语)。使用给定的参数分配这三个对象的值。请记住将两个AssignmentMarks对象分配给Student对象。最后,将学生添加到学生列表中。
学生班:
public class Student {
private int id;
private String firstName;
private String lastName;
public AssignmentMarks mathMarks;
public AssignmentMarks englishMarks;
public Student(int studentID, String firstName, String lastName, int
mathMark1, int mathMark2, int mathMark3, int englishMark1, int englishMark2,
int englishMark3) {
id = studentID;
this.firstName = firstName;
this.lastName = lastName;
}
}
分配标记类:
public class AssignmentMarks {
private String courseName;
private int assignment1;
private int assignment2;
private int assignment3;
public AssignmentMarks(String name, int mark1, int mark2, int mark3) {
courseName = name;
assignment1 = mark1;
assignment2 = mark2;
assignment3 = mark3;
}
}
学生对象将添加到列表中。我的问题是“在我提供的说明中,没有人知道“记住将两个AssignmentMarks对象分配给Student对象”是什么意思吗?
我理解这个问题,而不是怎么做。
答案 0 :(得分:1)
您必须创建两个AssignmentMarks
实例:
englishMarks
属性,mathMarks
属性。对于英文商标:
this.englishMarks = new AssignmentMarks("english",
englishMark1, englishMark2, englishMark3);
您对mathMarks也做同样的事情。
答案 1 :(得分:0)
似乎您想要或必须直接在此构造函数中处理提供给Student
的构造函数的参数。您可以通过在其中创建新的AssignmentMarks
并将其分配给Student
的类属性来实现。
没什么可做的,只需向Student
类添加两行,如下所示:
public class Student {
private int id;
private String firstName;
private String lastName;
public AssignmentMarks mathMarks;
public AssignmentMarks englishMarks;
public Student(int studentID, String firstName, String lastName,
int mathMark1, int mathMark2, int mathMark3,
int englishMark1, int englishMark2, int englishMark3) {
// assign the primitives and String attributes directly
id = studentID;
this.firstName = firstName;
this.lastName = lastName;
// assign the subject marks with newly created objects defined by parametrized constructor call
mathMarks = new AssignmentMarks("maths", mathMark1, mathMark2, mathMark3);
englishMarks = new AssignmentMarks("english", englishMark1, englishMark2, englishMark3);
}
}
答案 2 :(得分:-1)
两个对象AssignmentMarks mathMarks
和AssignmentMarks englishMarks
在构造函数中未分配,因此使用null
进行了初始化。但是,由于它们被声明为public
,因此可以通过创建的对象进行分配。看起来像这样:
Student s = new Student(0, "Foo", "Bar", 1, 2, 3, 4, 5, 6);
//Note that the arguments int mathMark1, int mathMark2,
//int mathMark3, int englishMark1, int englishMark2, int englishMark3 will be ignored
//s.mathMarks and s.englishMarks are null here
s.mathMarks = new AssignmentMarks("Math", 4, 5, 6);
//s.mathMarks is now initialized, s.englishMarks is not initialized.
s.englishMarks= new AssignmentMarks("English", 1, 2, 3);
//Both are initialized now.
希望对您有所帮助。
答案 3 :(得分:-1)
创建一个新的Student对象
Student student = new Student(id, firstName, lastName, mathsMark1, mathsMark2, mathsMark3, englishMark1, englishMark2, englishMark3);
和两个AsignmentMarks对象(用于数学和英语)
AssignmentMarks mathMarks = new AssignmentMarks("Mathematics", mathsMark1, mathsMark2, mathsMark3);
AssignmentMarks englishMarks = new AssignmentMarks("English", englishMark1, englishMark2, englishMark3);
记住要为学生分配两个AssignmentMarks对象 对象。
student.mathMarks = mathMarks;
student.englishMarks = englishMarks;
最后,将“学生”添加到学生列表中。
// List<Student> students = new ArrayList<>();
students.add(student);