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



在我的Java项目中,我有一个Java响应对象,它有几个私有字段及其getter和setter。

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

我想将成员分组到他们自己的类中,并使用Intellij重构将这些对象放在我的响应对象中,如下所示。响应对象在多个地方被使用,我无法手动重构它。我试图使用intellj重构/提取来实现这一点,但无法按照我想要的方式进行。如果我使用提取委托,结果会有所不同,这是我不希望的。感谢您的帮助。

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/ssetters(重构为字段中获得线索?

1( 在Response.java类中公开字段。

public String resp1Id;

2( 使用"inline…"重构访问器方法(包括getter和setter(intellij中的重构。(此步骤后的样本代码(

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

3( 将字段提取到委托类将使用字段创建新类。(此步骤后的样本代码(

public String resp1Id;

4( 重构-在新类中封装字段,使它们成为私有字段,并创建访问器方法。(此步骤后的样本代码(

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

最新更新