代码块和构造方法

代码块和构造方法

package com.sgg.day01;

class Father {
	static {
		System.out.println("111 111");
	}
	{
		System.out.println("222 222");
	}
	public Father() {
		System.out.println("333 333");
	}
}

public class Son extends Father {
	static {
		System.out.println("444 444");
	}
	{
		System.out.println("555 555");
	}
	public Son() {
		System.out.println("666 666");
	}
	public static void main(String[] args) {
		System.out.println("777 777");
		System.out.println("*** *** ***");
/*
 *	 	111 111
 * 		444 444
 * 		777 777
 * 		*** *** ***
 * 
 */
		new Son();
/*
 * 		222 222
 *		333 333
 * 		555 555
 *		666 666
 *
 */
		System.out.println("*** *** ***");
		new Son();
/*
* 		222 222
*		333 333
* 		555 555
*		666 666
*
*/	
		System.out.println("*** *** ***");
		new Father();
/*
 * 		222 222
 * 		333 333
 * 
 */
	}
}

package com.sgg.day01;
// 总结:由父及子,静态先行
class Root{
	static {
		System.out.println("Root的静态初始化块");
	}
	{
		System.out.println("Root的普通初始化块");
	}
	public Root() {
		super();
		System.out.println("Root的无参数的构造器");
	}
}

class Mid extends Root {
	static {
		System.out.println("Mid的静态初始化块");
	}
	{
		System.out.println("Mid的普通初始化块");
	}
	public Mid() {
		super();
		System.out.println("Mid的无参数的构造器");
	}
	public Mid(String msg) {
		// 通过 this 调用同一类中的构造器
		this();
		System.out.println("Mid的带参数构造器,其参数值:" + msg);
	}
}

class Leaf extends Mid{
	static {
		System.out.println("Leaf的静态初始化块");
	}
	{
		System.out.println("Leaf的普通初始化块");
	}
	public Leaf() {
		// 通过super调用父类中有一个字符串数的构造器
		super("尚硅谷");
		System.out.println("Leaf的构造器");
	}
}
public class LeafTest{
	public static void main(String[] args) {
		new Leaf();
		System.out.println("*** *** *** *** *** *** ***");
		new Leaf();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值