Java 从对象创建值的映射
我们有 及Java 从对象创建值的映射,java,Java,我们有 及 班级学生 { 列出姓名, 列出年龄, 列表专门化 } Students对象基本上是一个保存Student类字段值的结构 在不使用反射的情况下填充学生对象的最佳方法是什么 编辑:我们有一个特殊的要求,就是让学生上课,原因是我们并不总是想要学生课上的所有信息,如果我们有列表,它会为我们不感兴趣的字段分配内存。课堂学生{ class Students { List<String> names, List<Integer> age, Lis
班级学生
{
列出姓名,
列出年龄,
列表专门化
}
Students对象基本上是一个保存Student类字段值的结构
在不使用反射的情况下填充学生对象的最佳方法是什么
编辑:我们有一个特殊的要求,就是让学生上课,原因是我们并不总是想要学生课上的所有信息,如果我们有列表,它会为我们不感兴趣的字段分配内存。
课堂学生{
class Students
{
List<String> names,
List<Integer> age,
List<String> specialization
}
列出学生名单;
}
班级学生{
列出学生名单;
}
不要创建班级学生
。持有学生名单
Class Students {
List<Student> students;
}
作为旁注,您应该了解。不要创建类
学生
。持有学生名单
Class Students {
List<Student> students;
}
作为旁注,您应该了解。我不建议为此创建一个名为“学生”的类。您的目的是创建一个集合来保存Student对象 在这种情况下,请执行以下操作:
students.get(0).name;
List students=new ArrayList();
另外,请注意大小写:class是一个关键字,应该用小写字母拼写
查看venkat的评论后编辑:
如果您确实需要创建一个名为Students的类,那么以下内容应该可以使用(也可以使用上面另一个SO用户提供的类似答案):
班级学生{
List students=new ArrayList();
}
这应该行得通,但我强烈建议不要使用复数名称的此类类
PS:我是一名在大学教授编程语言的CS教授,长期担任开发人员/顾问。我不建议为此创建一个名为“学生”的课程。您的目的是创建一个集合来保存Student对象 在这种情况下,请执行以下操作:
students.get(0).name;
List students=new ArrayList();
另外,请注意大小写:class是一个关键字,应该用小写字母拼写
查看venkat的评论后编辑:
如果您确实需要创建一个名为Students的类,那么以下内容应该可以使用(也可以使用上面另一个SO用户提供的类似答案):
班级学生{
List students=new ArrayList();
}
这应该行得通,但我强烈建议不要使用复数名称的此类类
PS:我是一名在大学教授编程语言的CS教授,也是一名长期的开发人员/顾问。在这里寻找一个显而易见的答案
class Students {
List<Student> students = new ArrayList();
}
班级学生
{
列出姓名;
列出年龄;
清单专业化;
公立学生(名单学生){
学生(学生);
}
私立学校学生(列出学生){
name=students.stream
.map(学生::getName)
.collect(收集器.toList())
年龄=学生人数
.map(学生::getAge)
.collect(收集器.toList())
专业化=学生流
.map(学生::getSpecialization)
.collect(收集器.toList())
}
}
在这里寻找显而易见的答案
class Students {
List<Student> students = new ArrayList();
}
班级学生
{
列出姓名;
列出年龄;
清单专业化;
公立学生(名单学生){
学生(学生);
}
私立学校学生(列出学生){
name=students.stream
.map(学生::getName)
.collect(收集器.toList())
年龄=学生人数
.map(学生::getAge)
.collect(收集器.toList())
专业化=学生流
.map(学生::getSpecialization)
.collect(收集器.toList())
}
}
也许您想要使用装饰器模式(我不认为我节省了内存):
使用默认字段实现基类:
class Students
{
List<String> names;
List<Integer> age;
List<String> specialization;
public Student(List<Student> students) {
addStudents(students);
}
private void addStudents(List<Student> students) {
names = students.stream
.map(Student::getName)
.collect(Collectors.toList())
age = students.stream
.map(Student::getAge)
.collect(Collectors.toList())
specialization = students.stream
.map(Student::getSpecialization)
.collect(Collectors.toList())
}
}
添加默认界面:
public class BaseClass implements INameGettable {
protected String name;
public BaseClass(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
将装饰器添加到其他字段,例如年龄:
public interface INameGettable {
String getName();
}
用法:
public class Decorator implements INameGettable {
protected INameGettable nameable;
protected int age;
public Decorator(INameGettable nameable, int age) {
this.nameable = nameable;
this.age = age;
}
public String getName() {
return nameable.getName();
}
public int getAge() {
return this.age;
}
}
也许你想使用装饰图案(我不认为我节省了内存): 使用默认字段实现基类:
class Students
{
List<String> names;
List<Integer> age;
List<String> specialization;
public Student(List<Student> students) {
addStudents(students);
}
private void addStudents(List<Student> students) {
names = students.stream
.map(Student::getName)
.collect(Collectors.toList())
age = students.stream
.map(Student::getAge)
.collect(Collectors.toList())
specialization = students.stream
.map(Student::getSpecialization)
.collect(Collectors.toList())
}
}
添加默认界面:
public class BaseClass implements INameGettable {
protected String name;
public BaseClass(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
将装饰器添加到其他字段,例如年龄:
public interface INameGettable {
String getName();
}
用法:
public class Decorator implements INameGettable {
protected INameGettable nameable;
protected int age;
public Decorator(INameGettable nameable, int age) {
this.nameable = nameable;
this.age = age;
}
public String getName() {
return nameable.getName();
}
public int getAge() {
return this.age;
}
}
我想你真正想要的是列出学生是的,即使我离开了,你也需要在学生类中有一个学生对象列表,比如List students=new ArrayList();然后迭代学生从学生对象中获取每个元素,让它列出学生,这很好,我们有一个特殊的要求,就是让学生按原样上课,原因是我们并不总是想要学生类中的所有信息,如果我们有列表,它会为我们不感兴趣的字段分配内存,这将通过您想要的字段增强您的对象我认为您真正想要的是
列出学生
是的,即使我离开了,您也需要在学生类中有一个学生对象列表,如List students=new ArrayList();然后迭代学生从学生对象中获取每个元素,让它列出学生,这很好,我们有一个特殊的要求,就是让学生按原样上课,这是因为我们并不总是想要学生类中的所有信息,如果我们有列表,它会为我们不感兴趣的字段分配内存。除了使用装饰器模式或继承,这会增强你想要的字段的对象,我们有一个特殊的要求,就是让学生类保持原样,原因是我们并不总是想要学生课堂上的所有信息,如果我们有列表,它会分配mem