欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

继承与初始化[Java编程思想]

程序员文章站 2022-06-07 21:21:38
...

 

import static net.mindview.util.Print.*;

class Insect {
	private int i = 9;
	protected int j;

	Insect() {
		print("i="+i + ", j="+j);
		j=39;
	}

	private static int x1 = printInit("static Insect.x1 initialized");
	static int printInit(String s) {
		print(s);
		return 47;
	}
}

public class Beetle extends Insect {
	private int k = printInit("Beetle.k initialized");
	
	public Beetle() {
		print("k=" + k);
		print("j=" + j);
	}

	private static int x2 = printInit("static Beetle.x2 initialized");

	public static void mian(String[] args) {
		print("Beetle constructor");
		Beetle b = new Beetle();
	}
}

输出:
static Insect.x1 initialized
static Beetle.x2 initialized
Beetle constructor
i=9, j=0
Beetle.k initialized
k=47
j=39