ºìÁªLinuxÃÅ»§
Linux°ïÖú

½éÉÜǶÌ×ÀàºÍÄÚ²¿Àà

·¢²¼Ê±¼ä:2006-05-03 14:43:10À´Ô´:ºìÁª×÷Õß:reing
ÔÚÁíÒ»¸öÀàÖж¨ÒåµÄÀà¾ÍÊÇǶÌ×Àࣨnested classes£©¡£Ç¶Ì×ÀàµÄ·¶Î§ÓÉ×°ÈëËüµÄÀàµÄ·¶Î§ÏÞÖÆ¡£ÕâÑù£¬Èç¹ûÀàB±»¶¨ÒåÔÚÀàAÖ®ÄÚ£¬ÄÇôBΪAËùÖª£¬È»¶ø²»±»AµÄÍâÃæËùÖª¡£Ç¶Ì×Àà¿ÉÒÔ·ÃÎÊǶÌ×ËüµÄÀàµÄ³ÉÔ±£¬°üÀ¨private ³ÉÔ±¡£µ«ÊÇ£¬°üΧÀ಻ÄÜ·ÃÎÊǶÌ×ÀàµÄ³ÉÔ±¡£

ǶÌ×ÀàÒ»°ãÓÐ2ÖÖÀàÐÍ£ºÇ°Ãæ¼Óstatic±êʶ·ûµÄºÍ²»¼Óstatic ±êʶ·ûµÄ¡£Ò»¸östaticµÄǶÌ×ÀàÓÐstaticÐÞÊηû¡£ÒòΪËüÊÇstatic£¬ËùÒÔÖ»ÄÜͨ¹ý¶ÔÏóÀ´·ÃÎÊËü°üΧÀàµÄ³ÉÔ±¡£Ò²¾ÍÊÇ˵£¬Ëü²»ÄÜÖ±½ÓÒýÓÃËü°üΧÀàµÄ³ÉÔ±¡£ÒòΪÓÐÕâ¸öÏÞÖÆ£¬ËùÒÔstaticǶÌ×ÀàºÜÉÙʹÓá£

ǶÌ×Àà×îÖØÒªµÄÀàÐÍÊÇÄÚ²¿Àࣨinner class£©¡£ÄÚ²¿ÀàÊÇ·ÇstaticµÄǶÌ×Àà¡£Ëü¿ÉÒÔ·ÃÎÊËüµÄÍⲿÀàµÄËùÓбäÁ¿ºÍ·½·¨£¬Ëü¿ÉÒÔÖ±½ÓÒýÓÃËüÃÇ£¬¾ÍÏñÍⲿÀàÖеÄÆäËû·Çstatic³ÉÔ±µÄ¹¦ÄÜÒ»Ñù¡£ÕâÑù£¬Ò»¸öÄÚ²¿ÀàÍêÈ«ÔÚËüµÄ°üΧÀàµÄ·¶Î§Ö®ÄÚ¡£

ÏÂÃæµÄ³ÌÐòʾÀýÁËÈçºÎ¶¨ÒåºÍʹÓÃÒ»¸öÄÚ²¿Àà¡£ÃûΪOuter µÄÀàÓÐÒ»¸öÃûΪouter_x µÄʾÀý±äÁ¿£¬Ò»¸öÃûΪtest()µÄʵÀý·½·¨£¬²¢ÇÒ¶¨ÒåÁËÒ»¸öÃûΪInner µÄÄÚ²¿Àà¡£

// Demonstrate an inner class.
class Outer {
int outer_x = 100;

void test() {
Inner inner = new Inner();
inner.display();

}

// this is an inner class class Inner { void display() {System.out.println("display: outer_x = " + outer_x); }}}

class InnerClassDemo {

public static void main(String args[]) {
Outer outer = new Outer();
outer.test();

}
}

¸Ã³ÌÐòµÄÊä³öÈçÏÂËùʾ£º

display: outer_x = 100

ÔÚ±¾³ÌÐòÖУ¬ÄÚ²¿ÀàInner ¶¨ÒåÔÚOuter ÀàµÄ·¶Î§Ö®ÄÚ¡£Òò´Ë£¬ÔÚInner ÀàÖ®ÄÚµÄÈκδúÂë¿ÉÒÔÖ±½Ó·ÃÎʱäÁ¿outer_x ¡£ÊµÀý·½·¨display() ¶¨ÒåÔÚInner µÄÄÚ²¿£¬¸Ã·½·¨ÒÔ±ê×¼µÄÊä³öÁ÷ÏÔʾ outer_x ¡£InnerClassDemo µÄmain( ) ·½·¨´´½¨ÀàOuter µÄÒ»¸öʵÀý²¢µ÷ÓÃËüµÄtest( )·½·¨¡£´´½¨ÀàInner ºÍdisplay() ·½·¨µÄÒ»¸öʵÀýµÄ·½·¨±»µ÷Óá£

ÈÏʶµ½Inner ÀàÖ»ÓÐÔÚÀàOuter µÄ·¶Î§ÄÚ²ÅÊÇ¿ÉÖªµÄÊǺÜÖØÒªµÄ¡£Èç¹ûÔÚÀàOuter Ö®ÍâµÄÈκδúÂëÊÔͼʵÀý»¯Inner À࣬Java ±àÒëÆ÷»á²úÉúÒ»Ìõ´íÎóÏûÏ¢¡£×ÜÌåÀ´Ëµ£¬Ò»¸öǶÌ×ÀàºÍÆäËûÈκÎÁíÍâµÄ±à³ÌÔªËØûÓÐʲô²»Í¬£ºËü½ö½öÔÚËüµÄ°üΧ·¶Î§ÄÚÊÇ¿ÉÖªµÄ¡£

ÎÒÃǽâÊ͹ý£¬Ò»¸öÄÚ²¿Àà¿ÉÒÔ·ÃÎÊËüµÄ°üΧÀàµÄ³ÉÔ±£¬µ«ÊÇ·´¹ýÀ´¾Í²»³ÉÁ¢ÁË¡£ÄÚ²¿ÀàµÄ³ÉÔ±Ö»ÓÐÔÚÄÚ²¿ÀàµÄ·¶Î§Ö®ÄÚÊÇ¿ÉÖªµÄ£¬¶øÇÒ²»Äܱ»ÍⲿÀàʹÓá£ÀýÈ磺

// This program will not compile.
class Outer {
int outer_x = 100;

void test() {
Inner inner = new Inner();
inner.display();

}

// this is an inner class
class Inner {
int y = 10; // y is local to Inner
void display() {

System.out.println("display: outer_x = " + outer_x);
}
}

void showy() { System.out.println(y); // error£¬y not known here!}}

class InnerClassDemo {

public static void main(String args[]) {
Outer outer = new Outer();
outer.test();

}
}

ÕâÀyÊÇ×÷ΪInner µÄÒ»¸öʵÀý±äÁ¿À´ÉùÃ÷µÄ¡£ÕâÑù¶ÔÓÚ¸ÃÀàµÄÍⲿËü¾ÍÊDz»¿ÉÖªµÄ£¬Òò´Ë²»Äܱ»showy() ʹÓá£

¾¡¹ÜÎÒÃÇÇ¿µ÷ǶÌ×ÀàÔÚËüµÄÍⲿÀàµÄ·¶Î§Ö®ÄÚÉùÃ÷£¬µ«ÔÚ¼¸¸ö³ÌÐò¿éµÄ·¶Î§Ö®ÄÚ¶¨ÒåÄÚ²¿ÀàÊÇ¿ÉÄܵġ£ÀýÈ磬ÔÚÓÉ·½·¨¶¨ÒåµÄ¿éÖУ¬»òÉõÖÁÔÚfor Ñ­»·ÌåÄÚ²¿£¬ÄãÒ²¿ÉÒÔ¶¨ÒåǶÌ×À࣬ÈçÏÂÃæµÄ³ÌÐòËùʾ£º

// Define an inner class within a for loop.
class Outer {
int outer_x = 100;

void test() { for(int i=0; i<10; i++) { class Inner { void display() {System.out.println("display: outer_x = " + outer_x);

}
}
Inner inner = new Inner();
inner.display();

}
}
}

class InnerClassDemo {

public static void main(String args[]) {
Outer outer = new Outer();
outer.test();

}
}

¸Ã³ÌÐòµÄÕâ¸ö°æ±¾µÄÊä³öÈçÏÂËùʾ¡£

display: outer_x = 100
display: outer_x = 100
display: outer_x = 100

display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100

¾¡¹ÜǶÌ×ÀàÔÚÈÕ³£µÄ´ó¶àÊý±à³ÌÖв»Ê¹Ó㬵«µ±´¦Àíapplet £¨Ð¡Ó¦ÓóÌÐò£©Ê±ÊÇÌرðÓаïÖúµÄ¡£ÔÚµÚ20ÕÂÖÐÎÒÃǽ«¼ÌÐøǶÌ×ÀàµÄ»°Ìâ¡£ÔÚÄÇÀïÄ㽫¿´µ½¶ÔÓÚijЩÀàÐ͵ÄʼþÄÚ²¿ÀàÈçºÎ±»ÓÃÀ´¼ò»¯´úÂë¡£ÄãÒ²½«Á˽âÄäÃûÄÚ²¿Àࣨanonymous inner classes£©,ËüÊÇÒ»¸öûÓÐÃû×ÖµÄÄÚ²¿Àà¡£

×îºóÒ»µã£ºÇ¶Ì×ÀàÔÚJava µÄ×î³õµÄ1.0°æ±¾ÖÐÊDz»ÔÊÐíµÄ¡£Ö±µ½Java 1.1 ÖвÅÌí¼ÓÁËǶÌ×Àà¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ