我想知道,当我们调用由1指向的类动物(即initialize_animal()
)中定义的静态方法时,那么在执行主函数时输出为:Default height 0
。静态方法的工作
但是,如果我们将语句1放在语句2的下面,那么我们执行主函数输出为:Default Height 20
。
请告诉我在两个输出中这种差异的原因。
class test {
public static void main(String[] args) {
animal.initialize_animal(); // (1)
cat obj=new cat(); //2
System.out.println("Default height:" +obj.getheight());
}
}
class animal {
static int Height;
public animal() {
Height=0;
}
public int getHeight() {
return Height;
}
public static void initialize_animal() {
Height=20;
}
}
class cat extends animal {
String Sound;
Public cat() {
Sound="mew";
}
public String getSound() {
return Sound;
}
}
[标签:javascript]与[tag:java]无关。 –