W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
使用提取接口重構(gòu),您有下列三個(gè)選擇:
另外,在初始類中聲明的靜態(tài) final 字段可以被移動(dòng)到一個(gè)接口。因此,將會(huì)創(chuàng)建一個(gè)包含特定方法和字段的接口。從而,指定的類方法成為相應(yīng)接口方法的實(shí)現(xiàn)。
在這里,我們有一個(gè)類,并執(zhí)行提取接口重構(gòu),根據(jù)類的方法創(chuàng)建一個(gè)接口。
提取接口重構(gòu)的另一個(gè)例子,當(dāng)重命名的原始類和使用接口的可能選項(xiàng)被選中。
重構(gòu)前 | 重構(gòu)后 |
---|---|
|
您可以從已實(shí)現(xiàn)另一個(gè)接口的類中提取接口。讓我們從實(shí)現(xiàn) AnInterface 的類中提取接口。根據(jù)我們是否需要 AnotherInterface (提取的接口) 來擴(kuò)展 AnInterface (現(xiàn)有的界面), 或者我們希望源 AClass 實(shí)現(xiàn)它們, 我們將得到以下代碼:
提取的接口擴(kuò)展了現(xiàn)有的一個(gè):
您可以從已經(jīng)實(shí)現(xiàn)了另一個(gè)接口的類中提取接口。讓我們從實(shí)現(xiàn) AnInterface 的類中提取接口。根據(jù)我們是否需要 AnotherInterface (提取的接口)來擴(kuò)展 AnInterface (現(xiàn)有的接口)還是我們希望源代碼 AClass 來實(shí)現(xiàn)它們,我們將得到以下代碼:
提取的接口擴(kuò)展了現(xiàn)有的接口:
class AClass implements AnotherInterface {
public void publicMethod() {
//some code here
}
public void secretMethod() {
//some code here
}
}
提取的接口:
public interface AnotherInterface extends AnInterface {
}
Source 類實(shí)現(xiàn)兩個(gè)接口。
Source 類:
class AClass implements AnInterface, AnotherInterface {
public void publicMethod() {
//some code here
}
public void secretMethod() {
//some code here
}
}
提取的接口:
public interface AnotherInterface {
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: