Inner class

来源:互联网 发布:修改telnet端口 编辑:程序博客网 时间:2024/06/06 00:22

1.用外围类创建内部类对象时,此内部类对象会秘密的捕获一个指向外围类的引用,于是,可以通过这个引用来访问外围类的成员。 

2.  内部类中得到当前外围类对象的引用,可以使用.this关键字

3.create Inner class Object:

  1.  public class First {  
  2. public class Contents{  
  3.     public void f(){  
  4.         System.out.println("In Class First's inner Class Contents method f()");  
  5.     }  
  6.     public void getStr(){  
  7.         System.out.println("First.str="+str);  
  8.     }  
  9. }  
  10.   
  11. public static void main(String [] args){  
  12.     First first = new First();  
  13.     First.Contents contents = first.new Contents(); //创建内部类对象 
  14.     contents.f();  
  15. }  
  16.     }  

 必须通过外围类First的对象first来创建一个内部类的对象 
  而且需要注意的是,在创建外围类对象之前,不可能创建内部类的对象

4.内部类在接口中的应用

  1.  public interface Shape {  
  2. public void paint();  
  3.     }  
  4.     public class Painter {  
  5.       
  6.        private class InnerShape implements Shape{  
  7.     public void paint(){  
  8.         System.out.println("painter paint() method");  
  9.     }  
  10. }  
  11.   
  12. public Shape getShape(){  
  13.     return new InnerShape();  
  14. }     
  15.       
  16.        public static void main(String []args){  
  17.     Painter painter = new Painter();  
  18.     Shape shape = painter. getShape();  
  19.     shape.paint();  
  20. }  
  21.     }  
5.匿名内部类 

  1.  public class Painter {  
  2. ublic Shape getShape(){  
  3. return new Shape(){  
  4.     public void paint(){  
  5.         System.out.println("painter paint() method");  
  6.     }  
  7. };  
  8.   
  9.       public static void main(String [] args){  
  10.             Painter painter = new Painter();  
  11.             Shape shape = painter.getShape();  
  12.             shape.paint();  
  13.       }  
  14.   }  
  15.   public interface Shape {  
  16. ublic void paint();  
  17.   }  
  1.   public class B {  
  2. public A getA(final int num){  
  3.     return new A(num){  
  4.        public int getNum(){  
  5.                      return num;  
  6.                   }  
  7.     };  
  8. }  
  9.    }  
  10.    public class A {  
  11. private int num;  
  12. public A(int num){  
  13.     this.num = num;  
  14. }  
  15. public A(){  
  16.       
  17. }  
  18.    }  
  19. //可以为A的构造方法传入一个参数。在匿名内部类中,并没有使用到这个参数。 
      如果使用到了这个参数,那么这个参数就必须是final的。 


6.嵌套类
static的内部类就叫做嵌套类 
前面提到了很多次,嵌套类是个例外 
使用嵌套类时有两点需要注意: 
   a、创建嵌套类对象时,不需要外围类 
   b、在嵌套类中,不能像普通内部类一样访问外围类的非static成员 

  1.  public class StaticClass {  
  2. private int num;  
  3. private static int sum = 2;  
  4. private static class StaticInnerClass{  
  5.     public int getNum(){  
  6.     //只能访问sum,不能访问num  
  7.                return sum;  
  8.     }  
  9. }  
  10.   }  
  11.   public class Test {  
  12. public static void main(String [] args){  
  13.                //可以直接通过new来创建嵌套类对象  
  14.     StaticClass.StaticInnerClass inner = new StaticClass.StaticInnerClass();  
  15.     inner.getNum();  
  16. }  
  17.   }  

 9、为何要内部类? 
    a、内部类提供了某种进入外围类的窗户。 
    b、也是最吸引人的原因,每个内部类都能独立地继承一个接口,而无论外围类是否已经继承了某个接口。 
  因此,内部类使多重继承的解决方案变得更加完整。 
  在项目中,需要多重继承,如果是两个接口,那么好办,接口支持多重继承。 
  如果是两个类呢?这时只有使用内部类了。 
 
  1.  public interface One {  
  2. public void inOne();  
  3.    }  
  4.    public interface Two {  
  5. public void inTwo();  
  6.    }  
  7.    //两个接口,用普通类就可实现多重继承  
  8.    public class CommonClass implements One,Two {  
  9. public void inOne(){  
  10.     System.out.println("CommonClass inOne() method");  
  11. }  
  12.   
  13. public void inTwo(){  
  14.     System.out.println("CommonClass inTwo() method");  
  15. }  
  16.    }  
  17.    public abstract class Three {  
  18. public abstract void inThree();  
  19.    }  
  20.    public abstract class Four {  
  21. public abstract void inFour();  
  22.    }  
  23.    //两个抽象类,使用普通类无法实现多重继承  
  24.      
  25.    //使用内部类可以实现  
  26.    public class Contents extends Three {  
  27. public void inThree(){  
  28.     System.out.println("In Contents inThress() method");  
  29. }  
  30.   
  31. public class InnerFour extends Four{  
  32.     public void inFour(){  
  33.         System.out.println("In Contents");  
  34.     }  
  35.       
  36. }  
  37.    }  



0 0
原创粉丝点击