Java:thinking in java p241 exercise1

Create a simple class. Inside a second class, define a reference to an object of the first class. Use lazy initialization to instantiate this object.

 1 import static org.greggordon.tools.Print.*;
 2 
 3 class Engine {
 4     private String s;
 5     Engine() {
 6         println("Engine()");
 7         s = "Constructed";
 8     }
 9     public String toString() { return s; }
10 }
11 
12 public class Airplane {
13     private String fuselage, wings, tail;
14     private Engine e;
15     public Airplane() {
16         println("Inside Airplane()");
17         fuselage = "Body";
18         wings = "Airfoils";
19         tail = "Empennage";                
20     }
21     public String toString() { 
22         if(e == null) // lazy (delayed) initialization:
23             e = new Engine();
24         return "fuselage = " + fuselage + ", " +
25             "wings = " + wings + ", " +
26             "tail = " + tail + ", " +
27             "Engine = " + e;
28     }
29     public static void main(String[] args) {
30         Airplane N1234 = new Airplane();
31         print(N1234);
32     }
33 }

没看懂

posted @ 2015-04-21 20:22  陶修瑕  阅读(176)  评论(0编辑  收藏  举报