关于匿名内部类

本文探讨了Java中静态内部类的实例化方式,解释了如何从外部包构造静态内部类的实例,并澄清了一些常见误解。
12.   Which   statement   is   true?  
   
  A.   An   anonymous   inner   class   may   be   declared   as   final  
  B.   An   anonymous   inner   class   can   be   declared   as   private  
  C.   An   anonymous   inner   class   can   implement   multiple   interfaces  
  D.   An   anonymous   inner   class   can   access   final   variables   in   any   enclosing   scope  
  E.   Construction   of   an   instance   of   a   static   inner   class   requires   an   instance   of   the   enclosing   outer   class.  
-------------------------------------------------------------------------------------------------------------------------------------------------
13.   Given:  
   
  1.   package   foo;  
  2.      
  3.   public   class   Outer   (  
  4.         public   static   class   Inner   (  
  5.         )  
  6.   )  
   
  Which   statement   is   true?  
   
  A.   An   instance   of   the   Inner   class   can   be   constructed   with   “new   Outer.Inner   ()”  
  B.   An   instance   of   the   inner   class   cannot   be   constructed   outside   of   package   foo  
  C.   An   instance   of   the   inner   class   can   only   be   constructed   from   within   the   outer   class  
  D.   From   within   the   package   bar,   an   instance   of   the   inner   class   can   be   constructed   with   “new   inner()” 
--------------------------------------------------------------------------------------------------------------------------------------------------
这是一个static   inner   class.所以A是正确的。
--------------------------------------------------------------------------------------------------------------------------------------------------
/**  
  A.   An   instance   of   the   Inner   class   can   be   constructed   with   “new   Outer.Inner   ()”  
  B.   An   instance   of   the   inner   class   cannot   be   constructed   outside   of   package   foo  
  C.   An   instance   of   the   inner   class   can   only   be   constructed   from   within   the   outer   class  
  D.   From   within   the   package   bar,   an   instance   of   the   inner   class   can   be   constructed   with   “new   inner()”  
  */  
  package   foo;  
  public   class   Outer  
  {  
  public   static   class   Inner  
  {  
  public     void   writeSt()  
  {  
  System.out.println("Hello");  
  }  
  }  
  }  
   
  class   TryInner  
  {  
  public   TryInner()  
  {  
  new   Outer.Inner();   //-------------A   is   OK!!!!   and   C   is   wrong.  
  }  
  }  
  ***************  
  /**  
  A.   An   instance   of   the   Inner   class   can   be   constructed   with   “new   Outer.Inner   ()”  
  B.   An   instance   of   the   inner   class   cannot   be   constructed   outside   of   package   foo  
  C.   An   instance   of   the   inner   class   can   only   be   constructed   from   within   the   outer   class  
  D.   From   within   the   package   bar,   an   instance   of   the   inner   class   can   be   constructed   with   “new   inner()”  
  */  
  package   bar;  
  class   TryPack    
  {  
  public   static   void   main(String[]   args)    
  {  
  System.out.println("Hello   World!");  
  //new   Inner();   -----------This   line   is   wrong   so   D   is   wrong;  
  new   foo.Outer.Inner();   //   You   should   write   like   this.   and   B   is   wrong    
  }  
  }  
  ************************The   Answer   is   A
以下是对关于匿名内部类描述的分析: ### 可在定义类同时创建对象 匿名内部类适合创建那种只需要使用一次的类,在定义匿名内部类的同时就会创建对象,无需显式地再去创建对象。例如: ```java // 抽象类 abstract class Corporation { abstract void getFunction(); } class middleClass { public void getVariable(Corporation cp) { cp.getFunction(); } } public class testClass { public static void main(String[] args) { middleClass mc = new middleClass(); // 创建抽象类 Corporation 的匿名类并同时创建对象 mc.getVariable(new Corporation() { void getFunction() { System.out.println("匿名类:我是公司新加的人事部门,负责公司员工的调动与招聘"); } }); } } ``` 此描述正确,符合匿名内部类的特性,可在定义类的同时创建对象[^1][^3]。 ### 前提是继承父类或实现接口 匿名内部类必须继承一个父类,或实现一个接口,但最多只能继承一个父类,或者实现一个接口。例如: ```java // 接口 interface MyInterface { void doSomething(); } public class Main { public static void main(String[] args) { // 实现接口的匿名内部类 MyInterface obj = new MyInterface() { @Override public void doSomething() { System.out.println("匿名内部类实现接口方法"); } }; obj.doSomething(); } } ``` 此描述正确,创建匿名内部类需要继承父类或者实现接口[^1]。 ### 格式是'new 父类(参数列表)或父接口(){}' 创建匿名类的规则是:`new + 类名/接口名 + () + { 类体 }`。关键字`new`用来实例化;类名/接口名用来标识要继承类或实现的接口;`()`用来表示为匿名类的构造方法;`{ 类体 }`是匿名类定义的属性和行为。例如: ```java abstract class ParentClass { abstract void method(); } public class Test { public static void main(String[] args) { // 抽象类的匿名内部类 ParentClass pc = new ParentClass() { @Override void method() { System.out.println("匿名内部类重写抽象方法"); } }; pc.method(); } } ``` 此描述正确,符合匿名内部类的创建格式要求[^1][^2]。 ### 可作为方法参数传入 可以将匿名内部类作为方法参数传入,在方法中使用该匿名内部类对象调用相应的方法。例如: ```java abstract class Corporation { abstract void getFunction(); } class middleClass { public void getVariable(Corporation cp) { cp.getFunction(); } } public class testClass { public static void main(String[] args) { middleClass mc = new middleClass(); // 将匿名内部类作为参数传入方法 mc.getVariable(new Corporation() { void getFunction() { System.out.println("匿名类:我是公司新加的人事部门,负责公司员工的调动与招聘"); } }); } } ``` 此描述正确,匿名内部类可以作为方法参数传入[^3]。 综上所述,以上关于匿名内部类的描述都是正确的。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值