class Creature{
{
System.out.println("Creature的非静态初始化块");
}
public Creature(){
System.out.println("Creature的无参数构造器");
}
public Creature(String name){
//使用this调用另一个重载、无参数构造器
this();
System.out.println("Creature的带有name参数的构造器,name参数为:"+name);
}
}
class Animal extends Creature{
{
System.out.println("Animal的非静态初始化块");
}
public Animal(String name){
super(name);
}
public Animal(String name,int age){
this(name);
System.out.println("Animal的带有2个参数的构造器,age参数为:"+age);
}
}
class Wolf extends Animal{
{
System.out.println("Wolf的非静态初始化块");
}
public Wolf(){
//显式调用父类有2个参数的构造器
super("灰太狼",5);
System.out.println("Wolf的无参构造器");
}
public Wolf(double weight){
//使用this调用另一个构造器
this();
System.out.println("Wolf的带weight参数的构造器,weight参数为:"+weight);
}
}
public class InitTest {
public static void main(String[] args){
new Wolf(6.0);
}
}
|