在必须在所有方法中添加数据的方法之间传递对象

时间:2015-10-27 15:51:12

标签: java code-standards

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

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

1 个答案:

答案 0 :(得分:0)

你不需要返回对象

childMethod(propertyBean);

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