java 构造函数 调用顺序_Java多级继承中构造函数调用的顺序

//: c07:Sandwich.java

// Order of constructor calls.

// package c07;

// import com.bruceeckel.simpletest.*;

import java.util.*;

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()");}

}

public class Sandwich extends PortableLunch {

// private static Test monitor = new Test();

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();

/*

monitor.expect(new String[] {

"Meal()",

"Lunch()",

"PortableLunch()",

"Bread()",

"Cheese()",

"Lettuce()",

"Sandwich()"

});

// */

}

} ///:~

此代码的输出是

Meal()

Lunch()

PortableLunch()

Bread()

Cheese()

Lettuce()

Sandwich()

由于类中的字段是按照声明的顺序创建的,为什么不这样做

Bread()

Cheese()

Lettuce()

在上面列表的顶部?

另外,在此代码中它打算做什么?

monitor.expect(new String[] {

"Meal()",

"Lunch()",

"PortableLunch()",

"Bread()",

"Cheese()",

"Lettuce()",

"Sandwich()"

});

起初我以为这是一个匿名类,但看起来并不像它。它正在初始化String数组吗?为什么它没有String变量的名称?请告诉我这里使用的编程结构的名称。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值