`

JAVA编程思想第四版完整下载地址

阅读更多

http://hi.baidu.com/yang1101190/blog/item/beb856c34c47865eb219a875.html

 

http://blog.csdn.net/lcymsn/archive/2010/04/04/5449495.aspx

 

 

//: polymorphism/Sandwich.java
// Order of constructor calls.

//package polymorphism.Sandwich;

class Meal {
  static Meal ml = new Meal(1);
 // private Meal s = new Meal(2);  // stackOverFlow exception
  private Bread b = new Bread(3);  //若此句被注释掉,则输出为如下*****2
  Meal() { System.out.println("Meal()"); }
  Meal(int i) { System.out.println("Meal()" + i); }
}

class Bread {
  static Bread ml = new Bread(2);
  Bread() { System.out.println("Bread()"); }
  Bread(int i) { System.out.println("Bread()" + i); }
}

class Lunch extends Meal {
	//static Meal m2 = new Meal(2);
  Lunch() { System.out.println("Lunch()"); }
}

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

public class Sandwich extends PortableLunch {
  //static Bread bb = new Bread();  //这两句无论是否被保留,不影响输出
  //private Bread b = new Bread();
  public Sandwich() { System.out.println("Sandwich()"); }
  public static void main(String[] args) {
    new Sandwich();
  }
}

/**********1
Bread()2
Bread()3
Meal()1
Bread()3
Meal()
Lunch()
PortableLunch()
Sandwich()
*/


/***********2
Meal()1
Bread()2
Bread()
Meal()
Lunch()
PortableLunch()
Bread()
Sandwich()
*/

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics