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

子类实例化过程

程序员文章站 2022-05-24 21:06:05
...

一、静态资源加载
1.加载父类静态变量;
2.加载父类静态方法块;
3.加载子类静态变量;
4.加载子类静态方法块;
二、实例化资源加载
5.加载父类普通成员变量;
6.加载父类普通方法快;
7.加载父类构造函数;
8.加载子类普通成员变量;
9.加载子类普通方法块;
10.加载子类构造函数。

 package test;

public class Meal {     Meal() {        System.out.println("Meal()");   }       public static void main(String[] args) {        new Sandwich();     } }

class Bread {   public static Lettuce p=new Lettuce();  Bread() {       System.out.println("Bread()");  } }

class Cheese {  Cheese() {      System.out.println("Cheese()");     } }

class Lettuce {     Lettuce() {         System.out.println("Lettuce()");    } }

class Lunch extends Meal {  Lunch() {       System.out.println("Lunch()");  } }

class PortableLunch extends Lunch {     public static Lettuce p=new Lettuce();  PortableLunch() {       System.out.println("PortableLunch()");  } }

class Sandwich extends PortableLunch {  Bread b = new Bread();  public static Cheese c = new Cheese();  Lettuce l = new Lettuce();

    Sandwich() {        System.out.println("Sandwich()");   } }

运行结果:

Lettuce()
Cheese()
Meal()
Lunch()
PortableLunch()
Lettuce()
Bread()
Lettuce()
Sandwich()