Java基础之面向对象(五)--内部类

来源:互联网 发布:淘宝网妈妈装特价 编辑:程序博客网 时间:2024/05/19 19:32
[java] view plaincopy
  1. /* 
  2. 内部类的访问规则: 
  3. 1,内部类可以直接访问外部类中的成员,包括私有。 
  4.     之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用,格式 外部类名.this 
  5. 2,外部类要访问内部类,必须建立内部类对象。 
  6.  
  7. 访问格式: 
  8. 1,当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中。 
  9. 可以直接建立内部类对象。 
  10. 格式 
  11.     外部类名.内部类名  变量名 = 外部类对象.内部类对象; 
  12.     Outer.Inner in = new Outer().new Inner(); 
  13.  
  14. 2,当内部类在成员位置上,就可以被成员修饰符所修饰。 
  15.     比如,private:将内部类在外部类中进行封装。 
  16.         static:内部类就具备static的特性。 
  17.         当内部类被static修饰后,只能直接访问外部类中的static成员。出现了访问局限。 
  18.  
  19.         在外部其他类中,如何直接访问static内部类的非静态成员呢? 
  20.         new Outer.Inner().function(); 
  21.  
  22.         在外部其他类中,如何直接访问static内部类的静态成员呢? 
  23.         uter.Inner.function(); 
  24.  
  25.     注意:当内部类中定义了静态成员,该内部类必须是static的。 
  26.           当外部类中的静态方法访问内部类时,内部类也必须是static的。 
  27.  
  28.  
  29.  
  30.      
  31. 当描述事物时,事物的内部还有事物,该事物用内部类来描述。 
  32. 因为内部事务在使用外部事物的内容。 
  33.  
  34. class Body 
  35. { 
  36.     private class XinZang 
  37.     { 
  38.  
  39.     } 
  40.  
  41.     public void show() 
  42.     { 
  43.         new XinZang(). 
  44.     } 
  45.      
  46. } 
  47.  
  48.  
  49.  
  50.  
  51.  
  52. */  
  53. class Outer  
  54. {  
  55.     private static  int x = 3;  
  56.   
  57.       
  58.     static class Inner//静态内部类  
  59.     {  
  60.         static void function()  
  61.         {  
  62.             System.out.println("innner :"+x);  
  63.         }  
  64.     }  
  65.   
  66.     static class Inner2  
  67.     {  
  68.         void show()  
  69.         {  
  70.             System.out.println("inner2 show");  
  71.         }  
  72.     }  
  73.   
  74.     public static void method()  
  75.     {  
  76.         //Inner.function();  
  77.         new Inner2().show();  
  78.     }  
  79.   
  80. }  
  81.   
  82.   
  83. class  InnerClassDemo2  
  84. {  
  85.     public static void main(String[] args)   
  86.     {  
  87.         Outer.method();  
  88.         //Outer.Inner.function();  
  89.         //new Outer.Inner().function();  
  90.         //直接访问内部类中的成员。  
  91. //      Outer.Inner in = new Outer().new Inner();  
  92. //      in.function();  
  93.     }  
  94. }  

[java] view plaincopy
  1. /* 
  2. 内部类定义在局部时, 
  3. 1,不可以被成员修饰符修饰 
  4. 2,可以直接访问外部类中的成员,因为还持有外部类中的引用。 
  5.     但是不可以访问它所在的局部中的变量。只能访问被final修饰的局部变量。 
  6. */  
  7. class Outer  
  8. {  
  9.     int x = 3;  
  10.   
  11.     void method(final int a)  
  12.     {  
  13.         final int y = 4;  
  14.         class Inner  
  15.         {  
  16.             void function()  
  17.             {  
  18.                 System.out.println(y);  
  19.             }  
  20.         }  
  21.       
  22.         new Inner().function();  
  23.           
  24.     }  
  25. }  
  26.   
  27.   
  28. class  InnerClassDemo3  
  29. {  
  30.     public static void main(String[] args)   
  31.     {  
  32.         Outer out = new Outer();  
  33.         out.method(7);  
  34.         out.method(8);  
  35.     }  
  36.   
  37. }  

匿名内部类:

[java] view plaincopy
  1. /*   
  2. 匿名内部类: 
  3. 1,匿名内部类其实就是内部类的简写格式。 
  4. 2,定义匿名内部类的前提: 
  5.     内部类必须是继承一个类或者实现接口。 
  6. 3,匿名内部类的格式:  new 父类或者接口(){定义子类的内容} 
  7. 4,其实匿名内部类就是一个匿名子类对象。而且这个对象有点胖。  可以理解为带内容的对象。 
  8. 5,匿名内部类中定义的方法最好不要超过3个。 
  9.  
  10.  
  11. */  
  12. abstract class AbsDemo  
  13. {  
  14.     abstract void show();  
  15.       
  16. }  
  17.   
  18.   
  19. class Outer  
  20. {  
  21.     int x = 3;  
  22.   
  23.     /* 
  24.     class Inner extends AbsDemo 
  25.     { 
  26.         int num = 90; 
  27.         void show() 
  28.         { 
  29.             System.out.println("show :"+num); 
  30.         } 
  31.         void abc() 
  32.         { 
  33.             System.out.println("hehe"); 
  34.         } 
  35.     } 
  36.     */  
  37.   
  38.     public void function()  
  39.     {  
  40.         //AbsDemo a = new Inner();  
  41. //      Inner in = new Inner();  
  42. //      in.show();  
  43. //      in.abc();  
  44.       
  45.   
  46.         AbsDemo d = new AbsDemo()  
  47.         {  
  48.             int num = 9;  
  49.             void show()  
  50.             {  
  51.                 System.out.println("num==="+num);  
  52.             }  
  53.             void abc()  
  54.             {  
  55.                 System.out.println("haha");  
  56.             }  
  57.         };  
  58.   
  59.         d.show();  
  60.         //d.abc();//编译失败;  
  61.   
  62.           
  63.   
  64.   
  65.   
  66.     }  
  67. }  
  68.   
  69.   
  70.   
  71. class InnerClassDemo4   
  72. {  
  73.     public static void main(String[] args)   
  74.     {  
  75.         new Outer().function();  
  76.     }  
  77. }