请考虑以下代码
class Meal {
Meal() { System.out.println("Meal()"); }
}
class Bread {
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 {
PortableLunch() { System.out.println("PortableLunch()");}
}
class Sandwich extends PortableLunch {
private Bread b = new Bread();
private Cheese c = new Cheese();
private Lettuce l = new Lettuce();
public Sandwich() {
System.out.println("Sandwich()");
}
public static void main(String[] args) {
new Sandwich();
}
}
Run Code Online (Sandbox Code Playgroud)
基于我对类成员初始化和构造函数执行顺序的理解.我期待输出
Bread()
Cheese()
Lettuce()
Meal()
Lunch()
PortableLunch()
Sandwich()
Run Code Online (Sandbox Code Playgroud)
因为我相信甚至在主要方法被调用之前就已经初始化了班级成员.但是当我运行程序时,我有以下输出
Meal()
Lunch()
PortableLunch()
Bread()
Cheese()
Lettuce()
Sandwich()
Run Code Online (Sandbox Code Playgroud)
我的困惑是乳清Meal()Lunch()和PortableLunch()在Bread()Cheese()和Lettuce()之前运行,即使它们的构造函数在之后被调用.
Sot*_*lis 24
这些是实例字段
private Bread b = new Bread();
private Cheese c = new Cheese();
private Lettuce l = new Lettuce();
Run Code Online (Sandbox Code Playgroud)
它们仅在创建实例时存在(执行).
在你的程序中运行的第一件事是
public static void main(String[] args) {
new Sandwich();
}
Run Code Online (Sandbox Code Playgroud)
超级构造函数被隐式调用为每个构造函数中的第一个东西,即.之前System.out.println
class Meal {
Meal() { System.out.println("Meal()"); }
}
class Lunch extends Meal {
Lunch() { System.out.println("Lunch()"); }
}
class PortableLunch extends Lunch {
PortableLunch() { System.out.println("PortableLunch()");}
}
Run Code Online (Sandbox Code Playgroud)
在以后super()的调用,实例字段在构造函数代码之前再次实例化.
顺序,逆转
new Sandwich(); // prints last
// the instance fields
super(); // new PortableLunch() prints third
super(); // new Lunch() prints second
super(); // new Meal(); prints first
Run Code Online (Sandbox Code Playgroud)
即使他们的构造者在那里被召唤.
不是之后,这里的construstor方法看起来像编译器:
public Sandwich(){
super();// note this calls super constructor, which will call it's super and so on till Object's constructor
//initiate member variables
System.out.println("Sandwich()");
}
Run Code Online (Sandbox Code Playgroud)
我认为这里有两件事让你失望.第一个是main静态方法,其中成员变量b,c和l是非静态实例变量.这意味着它们属于类的对象,而不是类本身.因此,当初始化类以运行main方法时,不会调用Bread,Cheese和Lettuce的构造函数,因为没有创建三明治实例.
直到main实际运行,并且调用new Sandwich()是实际构造的任何对象.那时,操作的顺序是:
- 初始化基类的成员字段
- 运行基类(s)构造函数
- 初始化此类的成员字段
- 运行此类的构造函数
这是递归完成的,所以在这种情况下,顺序就是
- 膳食的初始字段(无)
- 运行Meal的构造函数(打印"Meal")
- 初始午餐领域(无)
- 运行午餐的构造函数(打印"午餐")
- PortableLunch的init字段(无)
- 运行PortableLunch的构造函数(打印"PortableLunch")
- 三明治的初始字段(打印"面包","奶酪"和"生菜")
- 运行三明治的构造函数(打印"三明治")
此顺序的目的是确保在运行子类中的任何代码之前完全初始化基类.这是必需的,因为在子类的构造函数内,它可以调用基类的方法.如果该基类没有首先初始化其成员,那么会发生不好的事情.
| 归档时间: |
|
| 查看次数: |
10067 次 |
| 最近记录: |