在 IntelliJ IDEA 中"将匿名转换为内部(Convert Anonymous to Inner)" 重构允许您将匿名类转换为命名的内部类。
示例
重构前
重构后
public class Class {
public Interface method() {
final int i = 0;
return new Interface() {
public int publicMethod() {
return i;}
};
}
}
public class Class {
public Interface method() {
final int i = 0;
return new MyInterfaceClass(i);
}
}
public class MyInterfaceClass implements Interface {
private final int
i;
public MyInterfaceClass(int i) {
this.i = i;
}
public int publicMethod() {
return
i;
}
}