public class Bird
{
private static int id = 0;
private String kind;
public Bird(String requiredKind)
{
id = id + 1;
kind = requiredKind;
}
public String toString()
{
return "Kind: " + kind + ", Id: " + id + "; ";
}
public static void main(String [] args)
{
Bird [] birds = new Bird[2];
birds[0] = new Bird("falcon");
birds[1] = new Bird("eagle");
for (int i = 0; i < 2; i++)
System.out.print(birds[i]);
System.out.println();
}
}
爲什麼這會返回Kind: falcon, Id: 2; Kind: eagle, Id: 2
我真的不知道嗎?鳥類[0]和鳥類[1]有不同的情況,它們的ID是2?爲什麼它不是1和1?爲什麼他們有相同的ID?
靜態變量是從類變量不同。從id中刪除「static」。 – 2012-01-03 03:55:22
@DarylTeo - 靜態變量與* instance *變量不同。它們與* class *變量相同。見:http://en.wikipedia.org/wiki/Class_variable – 2012-01-03 04:02:41
@lwburk有一個大腦屁=)。我的壞,謝謝。 – 2012-01-03 05:14:22