【发布时间】:2025-12-23 13:20:13
【问题描述】:
我在显示使用 Arrays 实用程序类添加到列表中的元素时遇到问题。值存储是来自下面显示的 People 类的对象
public class People {
private static Integer age;
private static String name;
public People(String name,Integer age){
this.age = age;
this.name = name;
}
public Integer getAge(){
return this.age;
}
public String getName(){
return this.name;
}
@Override
public String toString(){
return "name"+this.getName()+" Age"+ this.getAge();
}
}
通过以下代码将值添加到列表中:
List<People> myPeople = Arrays.asList(
new People("Samuel Owino", 18),
new People("Swale Mdohe", 12),
new People("Joseph Wambwile", 48),
new People("Samuel Werre", 18),
new People("Swale Mdohe", 12),
new People("Joseph Wambwile", 48)
);
显示代码如下:
myPeople.forEach(System.out::println);
意识到问题是在 bean 类中的实例变量、年龄和名称上使用静态访问修饰符,当删除静态时,该方法可以正常工作。感谢您的参考。
【问题讨论】:
-
那么您到底遇到了什么问题?