javaÖй¹Ô캯ÊýµÄµ÷ÓÃ˳Ðò
µ±Ò»¸ö¸´ÔӵĶÔÏó±»¹¹Ôìʱ,ËüµÄ¹¹Ô캯Êý°´ÏÂÃæµÄ˳Ðò±»µ÷ÓÃ(that the order of constructor calls for a complex object is as follows)
1.Æä»ùÀà(base-class)µÄ¹¹Ô캯Êý±»µ÷ÓÃ,Õâ¸ö²½ÖèÒԵݹéµÄ·½Ê½Öظ´,ËùÒÔ×îµ×²ã(the root of hierarchy)µÄ¹¹Ô캯ÊýÊ×Ïȱ»Ö´ÐÐ,È»ºóÊÇËüÉÏÒ»²ãÅÉÉúÀà(the next-derived class)...Ö±µ½×î¶¥²ãµÄÅÉÉúÀà(the most-derived class).
The base-class constructor is called. This step is repeated recursively such that the root of the hierarchy is constructed first, followed by the next-derived class, etc., until the most-derived class is reached.)
2.Èç¹ûÓаüº¬¹ØÏµ(composition),ÄÇôËüµÄ³ÉÔ±¶ÔÏó°´ÕÕÉùÃ÷µÄ˳Ðò±»¹¹Ôì.
Member initializers are called in the order of declaration.
3.ÅÉÉúÀ๹Ô캯ÊýµÄÄÚÈÝ(body)±»Ö´ÐÐ.
The body of the derived-class constructor is called.
Ò»¸öʵÀý:
class Cake{
Cake(){System.out.println("Cake()");}
}
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 {
//if make derived-class object as the menber of the base-class will lead a infinite
//loop and program will stop because of the memory consumed
//private Sandwich s=new Sandwich();
private Cake a=new Cake();
PortableLunch() {&n
Ïà¹ØÎĵµ£º
Java ÐòÁл¯¼ò½é
Java ¶ÔÏóÐòÁл¯ÊÇ JDK 1.1 ÖÐÒýÈëµÄÒ»×鿪´´ÐÔÌØÐÔÖ®Ò»£¬ÓÃÓÚ×÷ΪһÖÖ½« Java ¶ÔÏóµÄ״̬ת»»Îª×Ö½ÚÊý×飬ÒÔ±ã´æ´¢»ò´«ÊäµÄ»úÖÆ£¬ÒÔºó£¬ÈÔ¿ÉÒÔ½«×Ö½ÚÊý×éת»»»Ø Java ¶ÔÏóÔÓеÄ״̬¡£
ʵ¼ÊÉÏ£¬ÐòÁл¯µÄ˼ÏëÊÇ “¶³½á” ¶ÔÏó״̬£¬´«Êä¶ÔÏó״̬£¨Ð´µ½´ÅÅÌ¡¢Í¨¹ýÍøÂç´«ÊäµÈµÈ£©£¬È»ºó “½ ......
1.Çëд³öÏÂÁÐJava´úÂëµÄÊä³ö½á¹û
package test;
public class FatherClass()
{ public FatherClass()
&n ......
¡¡¡¡
ÔÚÃæÏò¶ÔÏó±à³ÌÖУ¬¶ÔÓÚÀ࣬¶ÔÏ󣬷½·¨£¬±äÁ¿µÈ·½ÃæµÄÃüÃûÊǷdz£Óм¼Çɵġ£±ÈÈ磬´óСдµÄÇø·Ö£¬Ê¹Óò»Í¬
×Öĸ¿ªÍ·µÈµÈ¡£µ«¾¿Æä±¾£¬×·ÆäÔ´£¬ÔÚΪһ¸ö×ÊÔ´ÆäÃû³ÆµÄʱºò£¬Ó¦¸Ã±¾×ÅÃèÊöÐÔÒÔ¼°Î¨Ò»ÐÔÕâÁ½´óÌØÕ÷À´ÃüÃû£¬²ÅÄܱ£Ö¤×ÊÔ´Ö®¼ä²»³åÍ»£¬²¢ÇÒÿһ¸ö¶¼±ãÓÚ¼Ç
Òä¡£
......
ѧÁ˼¯ºÏÀ࣬ÏëÓÃJavaµÄ¼¯ºÏÀà±àÒ»¸öͨѶ¼£¬¿ÉÊÇ·¢ÏÖÌâĿҪ¶ÔͨѶ¼ÖеÄÐÕÃûºÍ±àºÅ½øÐÐÅÅÐò¡£
²ËÄñ¸Õ¸ÕѧJava£¬±¾À´»¹Õæ²»ÖªµÀÓÐ±È½ÏÆ÷£¬ºÙºÙ£¬ÒÔΪJavaÊÇÈç´ËÇ¿´ó£¬¿ÉÒÔÖ±½Óµ÷ÓÃSort()·½·¨¾Í¿ÉÒÔ¶Ô¼¯ºÏÀàÖеĶÔÏó½øÐÐÅÅÐò£¬ºÙºÙ£¬ÓÚÊDZãÊÔÁËһϣ¬Ì«²»ºÃʹÁËѽ£¬ºÙºÙ£¬ÓÚÊÇÎÒ¿´ÁË¿´Ê飬µ½ÍøÉϲéÁ˲飬ÔÀ´ÒªÊµÏÖ±È½ÏÆ÷µÄ ......