嗨,這可能看起來像一個非常愚蠢的問題,但我最近進入了Java和我自己的構造函數。構造函數混淆Java
public class creatures {
private static String name;
private static int age;
private static String type;
public creatures(String name, int age, String type) {
this.name = name;
this.age = age;
this.type = type;
System.out.println("The creature's name is " + name + " \nThe creatures age is" + age + " \nThe creatures type is " + type);
}
public static void main(String [] args) {
creatures newcreature = new creatures("Zack", 100, "alien");
creatures newcreature1 = new creatures("Jonny", 500, "vampire");
creatures newcreature2 = new creatures("Dick", 4, "witch");
System.out.println(newcreature.name);
}
}
在我的主要方法的System.out.println
因此,印刷在構造後,我想通過引用我newcreature構造函數的名稱,打印名稱爲「扎克」,但它只是打印名稱「迪克」來自我所做的最後一個構造函數。我如何區分這些在同一個類中的構造函數?如果這是一個愚蠢的問題,再次抱歉。
爲什麼你所有的字段都是'靜態'?刪除。 – 2015-04-03 09:25:33
工作感謝!哇,我覺得很愚蠢 – 2015-04-03 09:29:10