²»ÖªµÀÓÐû½âÊͶÔ
µÇ¼ | ÂÛ̳µ¼º½ -> »ªÐÂÏÊÊ -> ÐÄÇéÏÐÁÄ | ±¾Ìû¹²ÓÐ 2 Â¥£¬·Ö 1 Ò³, µ±Ç°ÏÔʾµÚ 1 Ò³ : ±¾ÌûÊ÷ÐÎÁбí : ˢР: ·µ»ØÉÏÒ»Ò³
×÷ÕߣºÔÂ֮Ů¼À˾ (µÈ¼¶£º3 - ÂÔÖªÒ»¶þ£¬·¢Ìû£º164) ·¢±í£º2012-08-16 15:23:02¡¡ Â¥Ö÷¡¡ ¹Ø×¢´ËÌûÆÀ·Ö£º
¡¾ÆäËü»°Ìâ¡¿ÇóÖúÒ»¶Îjava codeСµÜÄ¿Ç°¸Õ¸ÕÉÏÊÖjava, ¿´µ½think in javaÀïÓÐÕâÒ»¶Îcode£¬ÈçÏ£º //: reusing/SprinklerSystem.java // Composition for code reuse. class WaterSource { private String s; WaterSource() { System.out.println("WaterSource()"); s = "Constructed"; } public String toString() { return s; } } public class SprinklerSystem { private String valve1, valve2, valve3, valve4; private WaterSource source = new WaterSource(); private int i; private float f; public String toString() { return "valve1 = " + valve1 + " " + "valve2 = " + valve2 + " " + "valve3 = " + valve3 + " " + "valve4 = " + valve4 + "\n" + "i = " + i + " " + "f = " + f + " " + "source = " + source; } public static void main(String[] args) { SprinklerSystem sprinklers = new SprinklerSystem(); System.out.println(sprinklers); } } ¡¡ /* Output: WaterSource() valve1 = null valve2 = null valve3 = null valve4 = null i = 0 f = 0.0 source = Constructed *///: ʵÔÚ²»¶®ÎªÊ²Ã´×îºóoutputÀïµÄsource=Constructed. ÌصØÇ°À (more...)
²»ÖªµÀÓÐû½âÊͶÔ
SprinklerSystemÀàÀïÃ涨ÒåÁËsoureÕâÒ»±äÁ¿
private WaterSource source = new WaterSource();
Ò²¾ÍÊÇ˵source Õâ¸ö±äÁ¿ÊôÓÚWaterSourceÀà
È»ºómain³ÌÐòÖÐSystem.out.println(sprinklers)»áµ÷ÓÃ
public String toString() {

return

"valve1 = " + valve1 + " " +

"valve2 = " + valve2 + " " +

"valve3 = " + valve3 + " " +

"valve4 = " + valve4 + "\n" +

"i = " + i + " " + "f = " + f + " " +

"source = " + source;

}

ÕâÀïÃæ"source = " + source;ÕâÒ»¾äÓÖ»áµ÷ÓÃsourceÕâÒ»±äÁ¿£¨ÊôÓÚWaterSourceÀࣩÖеÄ
public String toString() { return s; }
¶øsµÄ³õʼֵÊÇ Constructed
¡±Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it.¡±
»¶Ó­À´µ½»ªÐÂÖÐÎÄÍø£¬Ó»Ô¾·¢ÌûÊÇÖ§³ÖÎÒÃǵÄ×îºÃ·½·¨!Ô­ÎÄ / ´«Í³°æ / WAP°æËùÓлظ´´ÓÕâÀïÕ¹¿ªÊÕÆðÁбí
×÷ÕߣºÔÂ֮Ů¼À˾ (µÈ¼¶£º3 - ÂÔÖªÒ»¶þ£¬·¢Ìû£º164) ·¢±í£º2012-08-16 15:49:54¡¡ 2Â¥
лл ºÜÇå³þ¡° ÕâÀïÃæ"source = " + source;ÕâÒ»¾äÓÖ»áµ÷ÓÃsourceÕâÒ»±äÁ¿£¨ÊôÓÚWaterSourceÀࣩÖÐµÄ public String toString() { return s; } ¶øsµÄ³õʼֵÊÇ Constructed ¡± ¡¡ ²»¹ýÔÚ"source = " + source; ÕâÒ»¾äÀÈç¹ûµ÷ÓõÄÊÇsourceÕâÒ»±äÁ¿µÄtoString method£¬ ÄÇô²»Ó¦¸Ãд³É "source = " +source.toString(); ô£¿ ÄѵÀsource¿ÉÒÔ±»Ä¬ÈϳÉsource.toString()? ¡¡ PS:ÆäʵÕâÒ»ÕµıêÌâÊÇreusing class ºÍcomposition syntax, Ò»Ö±¸ß²»¶®Ê²Ã´Òâ˼£¿Ã»¿´³öÀ´Õâ¸öÀý×ÓºÍcomposition ÓÐʲô¹Øϵ¡£¡£ (more...)
ÎÒÒ²²»ÊǺÜÇå³þ Çó¸ßÊÖ°¡¡£¡£¡£¡£
²»¹ýÕâ¶Î´úÂëȷʵÊDZí´ï¡°compositionºÏ³É¡±µÄÒâ˼°É
¾ÍÊÇÀûÓÃÒÑÓеÄÀ࣬×é³ÉеÄÀà°¡
¾ÍÊÇSprinklerSystemÀïÓõ½ÁËwatersource
»¶Ó­À´µ½»ªÐÂÖÐÎÄÍø£¬Ó»Ô¾·¢ÌûÊÇÖ§³ÖÎÒÃǵÄ×îºÃ·½·¨!Ô­ÎÄ / ´«Í³°æ / WAP°æËùÓлظ´´ÓÕâÀïÕ¹¿ªÊÕÆðÁбí
ÂÛ̳µ¼º½ -> »ªÐÂÏÊÊ -> ÐÄÇéÏÐÁÄ | ·µ»ØÉÏÒ»Ò³ | ±¾Ö÷Ìâ¹²ÓÐ 2 ƪÎÄÕ£¬·Ö 1 Ò³, µ±Ç°ÏÔʾµÚ 1 Ò³ | »Øµ½¶¥²¿
<<ʼҳ¡¡ [1]¡¡ Ä©Ò³>>

ÇëµÇ¼ºó»Ø¸´£ºÕʺŠ¡¡ ÃÜÂë ¡¡