<pre style="font-family:Menlo;font-size:9pt;"><span style="color:#808080;">局部内部类和匿名内部类,具有相同的能力和作用,但局部内部类的名字在方法外是不可见的。
<pre style="font-family:Menlo;font-size:9pt;"><span style="color:#808080;">那么为什么我们使用局部内部类而不是匿名内部类呢?
<span style="color:#808080;"> 唯一理由是:我们需要一个命名的构造器或者需要重载构造器,而匿名内部类只能用于实例初始化.
<span style="color:#808080;"> 另一个理由:需要不止一个该内部类的对象.
<pre style="font-family:Menlo;font-size:9pt;"><span style="color:#808080;">
<pre style="font-family:Menlo;font-size:9pt;"><span style="color:#808080;">代码测试:
<pre style="font-family:Menlo;font-size:9pt;"><span style="color:#808080;">
public class LocalInnerClass {
private int count=0;
Counter getCounter(final String name){
//局部内部类
class LocalCounter implements Counter{
public LocalCounter(){
System.out.println("LocalCounter()");
}
public int next(){
System.out.println(name);
return count++;
}
}
return new LocalCounter();
}
Counter getCounter2(final String name){
return new Counter() {
@Override
public int next() {
System.out.println(name);
return count++;
}
//匿名内部类不能有带名字的构造器,只能有内容初始化;
{
System.out.println("Counter()");
}
};
}
public static void main(String[] args) {
LocalInnerClass localInnerClass=new LocalInnerClass();
Counter
c1=localInnerClass.getCounter("Local inner"),c2=localInnerClass.getCounter2("annomous class");
for(int i=0;i<7;i++){
System.out.println(c1.next());
}
for(int i=0;i<7;i++){
System.out.println(c2.next());
}
}
}
测试结果
<pre style="font-family:Menlo;font-size:9pt;"><span style="color:#808080;">
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|