Java 中的对象列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1623067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A list of objects in Java
提问by Mitciv
I'm trying to make a list containing different objects.
我正在尝试制作一个包含不同对象的列表。
List<Object> list = new ArrayList<Object>();
defObject defObj;
optObject optObj;
and defObject has just one property for a String.
defObject 只有一个字符串属性。
public static class defObject
{
public static String defObj;
public defObject(String x)
{
setDefObj(x);
}
public static String getDefObj() {
return defObj;
}
public static void setDefObj(String defObj) {
defObject.defObj = defObj;
}
}
if I add multiple defObjects to the list and go through the list after I'm done adding the element they all contain the same string, which was of the last defObject added to the list.
如果我将多个 defObjects 添加到列表中并在我添加完元素后遍历列表,它们都包含相同的字符串,这是添加到列表中的最后一个 defObject 的字符串。
I'm doing something like this to add the objects to the list:
我正在做这样的事情来将对象添加到列表中:
if (whatever)
list.add(defObj = new defObject("x"));
else if(whatever)
list.add(defObj = new defObject("y"));
and the result is two defObjects with a string of "y"
结果是两个带有“y”字符串的 defObjects
Please help me figure out why the objects aren't being added correctly and the properties are all same as the last defObj added to the list.
请帮我弄清楚为什么没有正确添加对象,并且属性都与添加到列表中的最后一个 defObj 相同。
采纳答案by Asaph
The problem is defObj
is static
so all instances are sharing the same variable. Remove the word static
from everywhere in your class and everything will likely work as you expect.
问题是,defObj
是static
如此的所有实例都共享同一个变量。static
从课堂上的任何地方删除这个词,一切都会如你所愿。
回答by Marc Müller
The String defObj
variable is static, so it's always equal for all instances of defObject. Remove the "static
" before your method and attribute declaration and it should work.
该String defObj
变量是静态的,因此对于 defObject 的所有实例它总是相等的。static
在您的方法和属性声明之前删除“ ”,它应该可以工作。
回答by OscarRyz
Replace:
代替:
public static class defObject
{
public static String defObj;
...
With:
和:
public static class defObject
{
public String defObj;
....
Or even better for:
或者甚至更好:
public class DefObject {
private String defObj;
....
Using the keyword static
will make the attribute or method a class
method, which means there will be only one for all the instances.
使用关键字 static
将使属性或方法成为class
方法,这意味着所有实例都只有一个方法。
Remove it from your code. Also notice in Java by convention the class name starts with uppercase and the opening brace is in the same line.
从您的代码中删除它。还要注意 Java 按照约定,类名以大写开头,并且左大括号在同一行中。
回答by non sequitor
After you remove static
from public static String defObj;
and make it private
you will also need to remove static
from your method signatures as static
methods can't access instance variables from a static context i.e. defObject.getDefObj()
can't access the instance variable defObj
since the compiler can't ensure that it already exists - no instance was instantiated and so no instance variable. This can only be done with static
properties on class load.
你删除之后static
从public static String defObj;
并使其private
您还需要删除static
从你的方法签名,static
方法不能从静态上下文的访问实例变量,即defObject.getDefObj()
不能访问实例变量defObj
,因为编译器不能保证它已经存在-不实例已实例化,因此没有实例变量。这只能通过static
类加载的属性来完成。