嗨,这可能看起来像一个非常愚蠢的问题,但我最近进入了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