简体   繁体   English

Intellij 将私有字段及其访问器方法移动/重构到不同的 class

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

In my Java project, i have a Java response object which has several private fields and their getters and setters.在我的 Java 项目中,我有一个 Java 响应 object 有几个私有字段及其获取器和设置器。

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

I want to group the members to their own classes and have those objects in my response object like below using Intellij refactoring.我想将成员分组到他们自己的类中,并在我的响应 object 中使用这些对象,如下所示,使用 Intellij 重构。 The response object is being used in several places, and I cannot refactor it manually.响应 object 正在多个地方使用,我无法手动重构它。 I tried to make it happen using intellj refactor/extract to class but could not do it the way i want to to be.我尝试使用 intellj 重构/提取到 class 来实现它,但无法按照我想要的方式做到这一点。 If I use extract-delegate, it comes out differently which I don't want.如果我使用 extract-delegate,结果会有所不同,这是我不想要的。 Any help is appreciated.任何帮助表示赞赏。

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
}

I achieved it by following the below steps.我按照以下步骤实现了它。 There is no single step refactoring tool available in Intellij. Intellij 中没有可用的单步重构工具。 [Took clue from Can IntelliJ refactor properties (get/setters) to fields? [从IntelliJ 能否将属性(get/setter)重构为字段?

1) Make field public in Response.java class. 1) 在 Response.java class 中公开字段。

public String resp1Id;

2) Refactor accessor methods (both getters & setters) using 'inline...' refactoring in intellij. 2)在intellij中使用'inline ...'重构访问器方法(getter和setter)。 (sample code after this step) (此步骤后的示例代码)

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

3) Extract field to delegate class New class will be created with fields. 3) 提取字段以委托 class 将使用字段创建新的 class。 (sample code after this step) (此步骤后的示例代码)

public String resp1Id;

4) Refactor - encapsulate fields in new class to make them private and create accessor methods. 4) 重构 - 将字段封装在新的 class 中,使其成为私有并创建访问器方法。 (sample code after this step) (此步骤后的示例代码)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM