匿名類轉(zhuǎn)換為內(nèi)部類
在 IntelliJ IDEA 中"將匿名轉(zhuǎn)換為內(nèi)部(Convert Anonymous to Inner)" 重構(gòu)允許您將匿名類轉(zhuǎn)換為命名的內(nèi)部類。
示例
重構(gòu)前 | 重構(gòu)后 |
---|
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;
}
}
|
如果要內(nèi)聯(lián)構(gòu)造函數(shù),請按照下列步驟操作:
- 將光標(biāo)放在要重構(gòu)的匿名類中。
- 在主菜單或選擇的上下文菜單上,選擇:重構(gòu)| 將匿名轉(zhuǎn)換為內(nèi)部。將打開 "將匿名轉(zhuǎn)換為內(nèi)部" 對話框。
- 在 "類名稱" 字段中,指定新內(nèi)部類的名稱。
- 在 "構(gòu)造函數(shù)參數(shù)" 區(qū)域中,選擇變量,這些變量將被用作內(nèi)部類構(gòu)造函數(shù)的參數(shù)。
- 單擊 "確定" 以創(chuàng)建內(nèi)部類。
更多建議: