Java 在方法之间传递一个对象,该对象必须在所有方法中不断添加数据

Java 在方法之间传递一个对象,该对象必须在所有方法中不断添加数据,java,code-standards,Java,Code Standards,我需要在方法之间传递对象并添加数据,如下例所示。下面写的代码是一个好的编程实践吗 public void parentMethod(){ PropertyBean propertyBean = new PropertyBean(); propertyBean.SetValue1("Value1"); propertyBean = childMethod(propertyBean); propertyBean.SetValue3("Value3"); } public PropertyBean c

我需要在方法之间传递对象并添加数据,如下例所示。下面写的代码是一个好的编程实践吗

public void parentMethod(){
PropertyBean propertyBean = new PropertyBean();
propertyBean.SetValue1("Value1");
propertyBean = childMethod(propertyBean);
propertyBean.SetValue3("Value3");
}
public PropertyBean childMethod(PropertyBean propertyBean){
propertyBean.SetValue2("Value2");
return propertyBean;
}

您不需要返回对象

childMethod(propertyBean);

此调用将确保为此对象设置Value2。在Java中,除了基本类型之外,大多数对象都是通过引用传递的

,除了propertyBean在parentMethod之后超出范围这一事实,这很好……感谢您的回复。我需要一些建议。在这里,childMethod中的代码将紧密耦合,无法轻松用于单元测试。那么你有什么改进代码的建议吗?谢谢你的回复。我需要一些建议。在这里,childMethod中的代码将紧密耦合,无法轻松用于单元测试。那么,您对改进这里的代码有什么建议吗?您的代码看起来不错。就像我说的,你不需要归还物品。另外,如果您只需要设置value2的值,我假设它是PropertyBean类的一个成员,那么只需为该成员使用setter方法即可。你熟悉“能手”和“二传手”的概念吗?