java----内部类,匿名内部类

   


内部类:

  1. public class B {  
  2. public A getA(int num){  
  3.     return new A(num){            
  4.     };  
  5. }  
  6. }
  7.   
  8. public class A {  
  9. private int num;  
  10. public A(int num){  
  11.     this.num = num;  
  12. }  
  13. public A(){        
  14. }  
  15. }  
  16.    



内部类:

  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. }  



匿名内部类:

  1. public class Painter {  
  2. public Shape getShape(){  
  3. return new Shape(){  
  4.     public void paint(){  
  5.         System.out.println("painter paint() method");  
  6.     }  //复写Shape接口的方法
  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. public void paint();  
  17. }  

注意,匿名内部类后面的分号不可缺少! 
  匿名类,顾名思义,就是没有名称。 
  getShape()方法里,就使用了匿名内部类。 

创建了一个实现了Shape的匿名类的对象。 
 匿名类可以创建,接口,抽象类,与普通类的对象。创建接口时,必须实现接口中所有方法



匿名内部类通过实例初始化,可以达到类似构造器的效果:

  1. public A getA(){  
  2. return new A(){  
  3. int num = 0;  
  4. String str;  
  5. {  
  6. str = "javaeye";  
  7. System.out.println("hello robbin");  
  8.  }  
  9. };  
  10. }  



用匿名内部类来改造工厂方法:

  1. public interface Service {  
  2. public void method1();  
  3. }  

  4. public interface ServiceFactory {  
  5. Service getService();  
  6. }  

  7. public class Implemention1 implements Service{  
  8. public void method1(){  
  9. System.out.println("In Implemention1 method method1()");  
  10. }  
  11.   
  12. public static ServiceFactory factory = new ServiceFactory(){  
  13. public Service getService(){  
  14.          return new Implemention1();  
  15. }  
  16. };  
  17. }  

  18. public class Implemention2 implements Service {  
  19. public void method1(){  
  20. System.out.println("in Implemention2 method method1()");  
  21. }  
  22.   
  23. public static ServiceFactory factory = new ServiceFactory(){  
  24.      public Service getService(){  
  25. return new Implemention2();  
  26. }  
  27. };  
  28. }  

  29. public class Test {  
  30. public static void main(String []args){  
  31. service(Implemention1.factory);  
  32. service(Implemention2.factory);        
  33. ServiceFactory factory1 = Implemention1.factory;  
  34. Service service1 = factory1.getService();  
  35. service1.method1();        
  36. ServiceFactory factory2 = Implemention1.factory;  
  37. Service service2 = factory2.getService();  
  38. service2.method1();  
  39. }  
  40. }  







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值