- public class Father {
- static {
- System.out.println("Father 静态块");
- }
- {
- System.out.println("Father 构造块");
- }
- public Father() {
- System.out.println("Father 构造器");
- }
- void func1() {
- System.out.println("Father 方法 1");
- }
- public static void main(String[] args) {
- Father father = new Son();
- father.func1();
- }
- }
- class Son extends Father {
- static{
- System.out.println("Son 静态块");
- }
- {
- System.out.println("Son 构造块");
- }
- public Son() {
- System.out.println("Son 构造器");
- }
- @Override
- void func1() {
- System.out.println("Son 方法 1");
- }
- void func2() {
- System.out.println("Son 方法 2");
- }
- }
结果:
Father 静态块
Son 静态块
Father 构造块
Father 构造器
Son 构造块
Son 构造器
Son 方法 1
来源: http://www.bubuko.com/infodetail-2839863.html