Java 将对象值存储在对象内
我想在我的对象中存储我的多重值,如下所示:Java 将对象值存储在对象内,java,list,object,Java,List,Object,我想在我的对象中存储我的多重值,如下所示: public class Student extends StudentModules{ //Initialization private String mName; private String mNICN; private String Module1; private int ExamScore1; private int AssignmentScore1; private String
public class Student extends StudentModules{
//Initialization
private String mName;
private String mNICN;
private String Module1;
private int ExamScore1;
private int AssignmentScore1;
private String Module2;
private int ExamScore2;
private int AssignmentScore2;
private String Module3;
private int ExamScore3;
private int AssignmentScore3;
如图所示,我想为每个学生创建一个模块,该模块与其分数关联:
Module 1 -> Exam1 -> Score1
Module 2 -> Exam2 -> Score2
Module 3 -> Exam3 -> Score3
这样才能获得每个学生每个模块的分数。
我目前能够在强制时工作(为每个模块创建变量)。所以我想创建一个对象来存储模块组及其分数
这是我的路径:
1. When user register, call Student class (Above)
2. Inside student class, I can create Module, with its score
所以当我访问学生对象时,我可以访问他们的分数,模块可能正在使用arraylist,所以我可以根据要采用的模块名称对其进行索引
无论如何,谢谢大家的帮助。那么,为什么不定义一个模块类并收集它们呢
class Module {
String name;
int score1;
int score2;
}
List<Module> modules;
类模块{
字符串名;
int评分1分;
int评分2分;
}
列出模块;
请看这个例子。您可以为需要的字段添加getter
class Student {
final List<Module> modules;
Student(List<Module> modules) {
this.modules = modules;
}
}
class Module {
private final String name;
private final int score1;
private final int score2;
Module(String name, int score1, int score2) {
this.name = name;
this.score1 = score1;
this.score2 = score2;
}
}
class MainTest {
public static void main(String[] args) {
Student andrewWatson = new Student(Arrays.asList(new Module("Module1", 95, 90), new Module("Module1", 65, 80)));
}
}
班级学生{
最后列出模块;
学生(列出模块){
这个。模块=模块;
}
}
类模块{
私有最终字符串名;
私人决赛积分1分;
私人决赛int成绩2分;
模块(字符串名称、整数分数1、整数分数2){
this.name=名称;
这1.score1=score1;
this.score2=score2;
}
}
类主测试{
公共静态void main(字符串[]args){
Student andrewWatson=新学生(Arrays.asList(新模块(“模块1”,95,90),新模块(“模块1”,65,80));
}
}
听起来你应该有另一个类,有一个字符串和两个int
s,然后有一个数组或它们的列表。你尝试了什么,什么不起作用?另外,让学生扩展StudentModules似乎是一个非常糟糕的设计,因为我认为我们不能将学生限定为StudentModules…是的,所以我想创建一个存储模块和分数的对象,这样我就可以单独访问它(每个模块每个分数)是的,所以我不能只访问模块
,我需要从Student->Module
,因此在您的例子中,我创建了一个类Module
,并在Student
内部(每个对象)。在Main Class
中,我将通过Student setter
调用,我如何实现它?请参见我添加的示例