簡體   English   中英

Intellij 將私有字段及其訪問器方法移動/重構到不同的 class

[英]Intellij move/refactor private fields and their accessor methods to different class

在我的 Java 項目中,我有一個 Java 響應 object 有幾個私有字段及其獲取器和設置器。

public class Response {
        String resp1Id;
        String resp1Message;
        String resp2Id;
        String resp2Message;
        //getters & setters
}

我想將成員分組到他們自己的類中,並在我的響應 object 中使用這些對象,如下所示,使用 Intellij 重構。 響應 object 正在多個地方使用,我無法手動重構它。 我嘗試使用 intellj 重構/提取到 class 來實現它,但無法按照我想要的方式做到這一點。 如果我使用 extract-delegate,結果會有所不同,這是我不想要的。 任何幫助表示贊賞。

public class Response {
        Resp1 resp1;
        Resp2 resp2Id;
        //getters & setters
}
public class Resp1 {
        String resp1Id;
        String resp1Message;
        //getters & setters
}
public class Resp2 {
        String resp2Id;
        String resp2Message;
        //getters & setters
}

我按照以下步驟實現了它。 Intellij 中沒有可用的單步重構工具。 [從IntelliJ 能否將屬性(get/setter)重構為字段?

1) 在 Response.java class 中公開字段。

public String resp1Id;

2)在intellij中使用'inline ...'重構訪問器方法(getter和setter)。 (此步驟后的示例代碼)

response.resp1Id = "abcdef";
String id = response.resp1Id;

3) 提取字段以委托 class 將使用字段創建新的 class。 (此步驟后的示例代碼)

public String resp1Id;

4) 重構 - 將字段封裝在新的 class 中,使其成為私有並創建訪問器方法。 (此步驟后的示例代碼)

response.getResp1().setResp1Id("ram");
String kumar = response.getResp1().getResp1Id();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM