/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2010, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
class Creature
{
{
System.out.println("Creature的非静态初始化块");
}
//下面定义两个构造器
public Creature()
{
System.out.println("Creature无参数的构造器");
}
public Creature(String name)
{
//使用this调用另一个重载的、无参数的构造器
this();
System.out.println("Creature带有nmae参数的构造器,name参数:"
+ name);
}
}
class Animal extends Creature
{
{
System.out.println("Animal的非静态初始化块");
}
public Animal(String name)
{
super(name);
System.out.println("Animal带一个参数的构造器,name参数:" + name);
}
public Animal(String name , int age)
{
//使用this调用另一个重载的构造器
this(name);
System.out.println("Animal带2个参数的构造器,其age:" + age);
}
}
class Wolf extends Animal
{
{
System.out.println("Wolf的非静态初始化块");
}
public Wolf()
{
//显式调用父类有2个参数的构造器
super("灰太狼", 3);
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(5.6);
}
}
/*
Creature的非静态初始化块
Creature无参数的构造器
Creature带有nmae参数的构造器,name参数:灰太狼
Animal的非静态初始化块
Animal带一个参数的构造器,name参数:灰太狼
Animal带2个参数的构造器,其age:3
Wolf的非静态初始化块
Wolf无参数的构造器
Wolf的带weight参数的构造器,weight参数:5.6
请按任意键继续. . .
*/
使用this调用另一个重载的构造器
最新推荐文章于 2021-04-09 15:30:11 发布