匿名内部类编译后的.class文件问题

就在附近 2013-04-24 02:39:27
编译后的.class文件中包括Implementation1$1.class,Implementation1$2.class,Implementation2$1.class,Implementation2$2.class,只有两个匿名内部类为什么会出现4个.class文件吗?

interface Service {
void method1();
void method2();
}

interface ServiceFactory {
Service getService();
}

class Implementation1 implements Service {
private Implementation1() {}
public void method1() {System.out.println("Implementation1 method1");}
public void method2() {System.out.println("Implementation1 method2");}
public static ServiceFactory factory =
new ServiceFactory() {
public Service getService() {
return new Implementation1();
}
};
}

class Implementation2 implements Service {
private Implementation2() {}
public void method1() {System.out.println("Implementation2 method1");}
public void method2() {System.out.println("Implementation2 method2");}
public static ServiceFactory factory =
new ServiceFactory() {
public Service getService() {
return new Implementation2();
}
};
}

public class Factories {
public static void serviceConsumer(ServiceFactory fact) {
Service s = fact.getService();
s.method1();
s.method2();
}
public static void main(String[] args) {
serviceConsumer(Implementation1.factory);
// Implementations are completely interchangeable:
serviceConsumer(Implementation2.factory);
}
} /* Output:
Implementation1 method1
Implementation1 method2
Implementation2 method1
Implementation2 method2
*///:~
...全文
635 6 打赏 收藏 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
csnd2012 2015-06-25
  • 打赏
  • 举报
回复
我把你的Implementation1和Implementation2改成Class1,Class2,编译后如下图(Outin和OuiInTest是同个包的,请直接无视)


并没有出现两个匿名内部类的情况,不知你是什么情况
xb12369 2013-04-24
  • 打赏
  • 举报
回复
可能是开发环境不一样吧 像我这个明显就是 冲突代码 他(svn)不会删你的东西,知识多给你一个版本
就在附近 2013-04-24
  • 打赏
  • 举报
回复
引用 3 楼 xb12369 的回复:
我也有这玩意,原因就是我更新了代码,然后别人的代码跟我有冲突,我没有解决 直接编译 完了后在class就会有两个 Thinking in Java不晓得是啥,没有用过!
你可以把上面的代码复制到JVM上运行一下,并不是代码有冲突,是因为有匿名内部类
xb12369 2013-04-24
  • 打赏
  • 举报
回复


我也有这玩意,原因就是我更新了代码,然后别人的代码跟我有冲突,我没有解决

直接编译

完了后在class就会有两个

Thinking in Java不晓得是啥,没有用过!
就在附近 2013-04-24
  • 打赏
  • 举报
回复
引用 1 楼 xb12369 的回复:
从svn上拉下来的吧? 又不一样的东西,然后没有处理! 一般是可以直接删掉的!不影响!
是Thinking in Java里的代码,我复制到netbeans里运行的
xb12369 2013-04-24
  • 打赏
  • 举报
回复
从svn上拉下来的吧? 又不一样的东西,然后没有处理! 一般是可以直接删掉的!不影响!

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧