»áÔ±£º ÃÜÂ룺 ¡¡Ãâ·Ñ×¢²á | Íü¼ÇÃÜÂë | »áÔ±µÇ¼ ÍøÒ³¹¦ÄÜ£º ¼ÓÈëÊÕ²Ø ÉèΪÊ×Ò³ ÍøÕ¾ËÑË÷  
 °²È«¼¼Êõ¼¼ÊõÎĵµ
  ¡¤ °²È«ÅäÖÆ
  ¡¤ ¹¤¾ß½éÉÜ
  ¡¤ ºÚ¿Í½Ìѧ
  ¡¤ ·À»ðǽ
  ¡¤ Â©¶´·ÖÎö
  ¡¤ ÆƽâרÌâ
  ¡¤ ºÚ¿Í±à³Ì
  ¡¤ ÈëÇÖ¼ì²â
 °²È«¼¼ÊõÂÛ̳
  ¡¤ °²È«ÅäÖÆ
  ¡¤ ¹¤¾ß½éÉÜ
  ¡¤ ·À»ðǽ
  ¡¤ ºÚ¿ÍÈëÇÖ
  ¡¤ Â©¶´¼ì²â
  ¡¤ Æƽⷽ·¨
  ¡¤ É±¶¾×¨Çø
 °²È«¼¼Êõ¹¤¾ßÏÂÔØ
  ¡¤ É¨Ã蹤¾ß
  ¡¤ ¹¥»÷³ÌÐò
  ¡¤ ºóÃÅľÂí
  ¡¤ ¾Ü¾ø·þÎñ
  ¡¤ ¿ÚÁîÆƽâ
  ¡¤ ´úÀí³ÌÐò
  ¡¤ ·À»ðǽ
  ¡¤ ¼ÓÃܽâÃÜ
  ¡¤ ÈëÇÖ¼ì²â
  ¡¤ ¹¥·ÀÑÝʾ
¼¼ÊõÎĵµ > ÆäËûÎĵµ
È«ÊÀ½çËùÓгÌÐòÔ±¶¼»á·¸µÄ´íÎó
·¢±íÈÕÆÚ£º2003-03-16 00:00:00×÷Õߣº² ³ö´¦£º  

µ±Ä꣬¹ú¼Ê¾ÞÐdzÉÁúµÄ¡¸ÁúÖÖ¡¹Æع⣬ÖÚÈËÖ¸ÔðËû¶Ô²»Æð½¿ÆÞÁַソ£¬±ÆµÃËû³öÃæÕÙ¿ª¼ÇÕ߻ᣬÏòÊÀÈË×Ô°×Ëû·¸ÁË¡¸È«ÊÀ½çËùÓÐÄÐÈ˶¼»á·¸µÄ´íÎ󡹡£´ÓÀ´Ã»·¸¹ýÕâÖÖ´íÎóµÄÎÒ£¬Ò²Òò´Ë³£³£ÈÏΪ×Ô¼º²»ÊǸöÄÐÈË¡£

ËäȻû·¸¹ý¡¸È«ÊÀ½çËùÓÐÄÐÈ˶¼»á·¸µÄ´íÎó¡¹£¬µ«ÊÇÎÒµ¹ÊÇÔø¾­·¸ÁË¡¸È«ÊÀ½çËùÓгÌÐòÔ±¶¼»á·¸µÄ´íÎ󡹡£²»¹ÜʹÓúÎÖÖÓïÑÔ£¬È«ÊÀ½çËùÓгÌÐòÔ±¶¼Ò»¶¨·¸¹ýÕâÖÖ´íÎó£¬ÄǾÍÊÇ£ºÌ«ÒÀÀµ±àÒëÆ÷£¬È´²»ÖªµÀ±àÒëÆ÷×öÁËÄÄЩÊ¡£

Ò»°ãÀ´Ëµ£¬Ô½¸ß½×µÄ³ÌÐòÓïÑÔ£¬»áÌṩԽ¶àÓï·¨ÉϵıãÀû£¬ÒÔ·½±ã³ÌÐò׫д£¬Õâ¾ÍË׳ÆΪsyntactic sugar£¬ÎÒ³ÆÆäΪ¡¸Óï·¨ÉϵÄÌðÍ·¡¹¡£Ëä˵ÊÇÌðÍ·£¬µ«ÊÇÈç¹ûÄãδÄÜÁ˽â¸ÃÓï·¨µÄʵÖÊÄÚº­£¬ºÜ¿ÉÄÜ»áδ³¢ÌðÍ·£¬È´³Ô¾¡¿àÍ·¡£

²»¾ÃÇ°£¬ÎÒÊÕµ½Ò»¸öµç×ÓÓʼþ£¬¶ÁÕßÁгöÏÂÃæµÄJava³ÌÐò£¬ÏòÎÒÇó¾È¡£¿´¹ýÕâ¸ö³ÌÐòÖ®ºó£¬ÎÒÈ·¶¨ÕâÓÖÊÇÒ»¸ö¡¸È«ÊÀ½çËùÓгÌÐòÔ±¶¼»á·¸µÄ´íÎ󡹡£

// ³ÌÐò1

class Singleton {

 private static Singleton obj = new Singleton();

 public static int counter1;

 public static int counter2 = 0;

 private Singleton() {

  counter1++;

  counter2++;

 }

 public static Singleton getInstance() {

  return obj;

 }

}

// ³ÌÐò2

public class MyMain {

 public static void main(String[] args) {

  Singleton obj = Singleton.getInstance();

  System.out.println("obj.counter1=="+obj.counter1);

  System.out.println("obj.counter2=="+obj.counter2);

 }

}

Ö´Ðнá¹ûÊÇ£º

obj.counter1==1

obj.counter2==0

ÄãÓÐûÓб»´Ë½á¹ûÏÅÒ»Ìø£¿Õ§¿´³ÌÐò´úÂ룬ÄãºÜ¿ÉÄÜ»áÈÏΪcounter1ºÍcounter2µÄÖµÒ»¶¨»áÏàµÈ£¬µ«Ö´Ðнá¹ûÏÔÈ»²»ÊÇÈç´Ë¡£Æäʵ£¬³ÌÐò1±»±àÒëºóµÄ³ÌÐòÓ¦¸ÃµÈͬÓÚÏÂÃæµÄ³ÌÐò3£º

// ³ÌÐò3

class Singleton {

 private static Singleton obj;

 public static int counter1;

 public static int counter2;

 static { // Õâ¾ÍÊÇclass constructor

  // ÔÚ½øÈë´Ëclass constructor֮ǰ£¬classÒѾ­±»JVM

  // ÅäÖúÃÄڴ棬ËùÓеÄstatic field¶¼»á±»ÏÈÉ趨Ϊ0£¬

  // ËùÒÔ´Ëʱcounter1ºÍcounter2¶¼ÒѾ­ÊÇ0£¬ÇÒsingletonΪnull

  obj = new Singleton(); // ÎÊÌâ½ÔÓÉ´ËÐгÌÐò²úÉú

  // counter1²»»áÔڴ˱»É趨Ϊ0

  counter2 = 0; // counter2ÔÙ±»É趨һ´Î0£¨ÆäʵÊǶà´ËÒ»¾Ù£©

 }

 private Singleton() { // ÕâÊÇinstance constructor

  counter1++;

  counter2++;

 }

 public static Singleton getInstance() {

  return obj;

 }

}

ÕâÊÇÒòΪ£ºµ±class¾ßÓÐstatic field£¬ÇÒÖ±½ÓÔÚÐû¸æ´¦Í¸¹ý¡¸=...¡¹µÄ·½Ê½É趨Æäֵʱ£¬±àÒëÆ÷»á×Ô¶¯½«ÕâЩÐðÊöÒÀÐò°áµ½class constructorÄÚ¡£Í¬ÑùµØ£¬µ±class¾ßÓÐinstance field£¬ÇÒÖ±½ÓÔÚÐû¸æ´¦Í¸¹ý¡¸=...¡¹µÄ·½Ê½É趨Æäֵʱ£¬±àÒëÆ÷»á×Ô¶¯½«ÕâЩÐðÊöÒÀÐò°áµ½instance constructorÄÚ¡£

´Ë³ÌÐòÔÚclass constructorÄÚ£¬»¹Î´½«static field³õʼ»¯Ê±£¨Õâʱºò£¬counter1ºÍcounter2¶¼ÊÇ0£©£¬¾Íºô½Ðinstance constructor£¬¶øinstance constructor¾¹È»»¹»áÈ¥¸ü¶¯static fieldµÄÖµ£¬Ê¹µÃcounter1ºÍcounter2¶¼±ä³É1¡£È»ºóinstance constructorÖ´ÐÐÍ꣬»Øµ½class constructor£¬ÔÙ°Ñcounter2µÄÖµÉèΪ0£¨µ«ÊÇ

counter1ά³Ö²»±ä£©¡£×îºóµÄ½á¹û£ºcounter1µÈÓÚ1£¬counter2µÈÓÚ0¡£

Óû¸ÄÕý³ÌÐò1£¬·½·¨ÓÐÈý£º

-·½·¨Ò»£º½«singleton fieldµÄÐû¸æµ÷µ½counter1Óëcounter2 fieldÖ®ºó¡£

         ÕâÊÇ×îºÃµÄ×÷·¨¡£

-·½·¨¶þ£º½«counter2=0µÄÐû¸æÖУ¬¡¸=0¡¹µÄ²¿·Öɾ³ý¡£ÕâÖÖ×÷·¨Ö»ÓÐÔÚÏ£Íû

-·½·¨Èý£º½«³õʼ»¯µÄ¶¯×÷°áµ½class constructorsÄÚ£¬×ÔÐÐ׫д£¬¶ø²»ÒÀÀµ

         ±àÒëÆ÷²úÉú¡£ÕâÊÇ×î±£ÏÕµÄ×÷·¨¡£

ÈçºÎ±ÜÃⷸϡ¸È«ÊÀ½çËùÓгÌÐòÔ±¶¼»á·¸µÄ´íÎó¡¹£¬ÎÒ¸ø¸÷λJava³ÌÐòÔ±

µÄ½¨ÒéÊÇ£º

-Êì¶ÁJava Language Specification

-ÔÚÓÐÒÉÎÊʱ£¬Ê¹ÓÃJ2SDKËùÌṩµÄjavapÀ´·´×éÒëJava Bytecode£¬Ö±½Ó¹Û²ì

±àÒëºóµÄ½á¹û¡£

ÏÂÃæÊÇÎÒÓÃjavapÀ´·´×éÒë³ÌÐò1µÄʾ·¶£º

C:\>javap -c -classpath . Singleton

Compiled from MyMain.java

class Singleton extends java.lang.Object {

  public static int counter1;

  public static int counter2;

  public static Singleton getInstance();

  static {};

}

Method Singleton()

  0 aload_0

  1 invokespecial #1 <Method java.lang.Object()>

  4 getstatic #2 <Field int counter1>

  7 iconst_1

  8 iadd

  9 putstatic #2 <Field int counter1>

 12 getstatic #3 <Field int counter2>

 15 iconst_1

 16 iadd

 17 putstatic #3 <Field int counter2>

 20 return

Method Singleton getInstance()

  0 getstatic #4 <Field Singleton obj>

  3 areturn

Method static {}

  0 new #5 <Class Singleton>

  3 dup

  4 invokespecial #6 <Method Singleton()>

  7 putstatic #4 <Field Singleton obj>

 10 iconst_0

 11 putstatic #3 <Field int counter2>

 14 return

ÆäʵJavaµÄsyntactic sugar²¢²»Ëã¶à£¬C#µÄsyntactic sugar²ÅÕæµÄÊÇÎÞËù²»ÔÚ£¬

Ò²Òò´ËC#µÄ³õѧÕ߸üÈÝÒ×·¸ÁË¡¸È«ÊÀ½çËùÓгÌÐòÔ±¶¼»á·¸µÄ´íÎ󡹡£Ðí¶àC#µÄÊ鶼»áÒ»±ß½éÉÜC#Óï·¨£¬Ò»±ß½éÉܱàÒëÖ®ºóMSIL£¨.NETµÄÖмäÓïÑÔ£¬ÀàËÆJavaµÄBytecode£©µÄ½á¹û£¬È»¶øJavaµÄÊéÈ´ÏÊÉÙÕâô×ö¡£

Ëä˵ÊÇ¡¸È«ÊÀ½çËùÓгÌÐòÔ±¶¼»á·¸µÄ´íÎó¡¹£¬µ«ÊÇÕâ²»´ú±íÄã·¸ÁË´Ë´íÎóÖ®ºó£¬ÈÔ¿ÉÒÔͬ°®½èÇ®µÄ²ÜÆôÌ©Ò»°ãµØ¡¸Ì§Í·Í¦ÐØ¡¢ÀíÖ±Æø׳¡¹¡£Ö»ÒªÓÐÐÄ£¬ÆäʵÕâÒ»ÀàµÄ´íÎóÈÔÊÇ¿ÉÒÔ±ÜÃâµÄ¡£

¡¾·µ»Ø¶¥²¿¡¿ ¡¾´òÓ¡±¾Ò³¡¿ ¡¾¹Ø±Õ´°¿Ú¡¿

¹ØÓÚÎÒÃÇ / ¸øÎÒÁôÑÔ / °æȨ¾Ù±¨ / Òâ¼û½¨Òé / ÍøÕ¾±à³ÌQQȺ   
Copyright ©2003- 2024 Lihuasoft.net webmaster(at)lihuasoft.net ¼ÓÔØʱ¼ä 0.00146