哥们这句话
new Wolf("灰太狼",32.3)//意思是调用了Wolf类的构造函数,
在下边的这段代码中..
public class Wolf extends Animal{
private String name;
private double weight;
public Wolf(String name, double weight){
this.name = name;
this.weight = weight;
}
//重写父类的getDesc()方法
@Override
public String getDesc(){
return "Wolf[name=" + name + ",weight=" + weight + "]";
}
public static void main(String[] args){
System.out.println(new Wolf("灰太狼",32.3));
}
}
是按照顺序执行的,进入入口后new了个Wolf类对象,进入类中,先是属性的默认初始化,构造代码块初始化..
重点在这: 你后边还有个return语句,它返回的值是默认的初始化值,并且记录下来...
所以在你输出时是默认的初始化值..
也就是Wolf[name=null,weight=0.0] |