0

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

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;
}
4

1 回答 1

0

你不需要返回对象

childMethod(propertyBean);

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

于 2015-10-27T15:54:26.190 回答