在必须在所有方法中不断添加数据的方法之间传递对象
Passing an object between the methods which has to keep adding data in all the methods
我需要在方法之间传递对象并添加数据,如下例所示。下面编写的代码是一个好的编程习惯吗?
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;
}
你不需要return对象
childMethod(propertyBean);
此调用将确保为此对象设置了 Value2。大多数对象在 Java 中通过引用传递(基本类型除外)
我需要在方法之间传递对象并添加数据,如下例所示。下面编写的代码是一个好的编程习惯吗?
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;
}
你不需要return对象
childMethod(propertyBean);
此调用将确保为此对象设置了 Value2。大多数对象在 Java 中通过引用传递(基本类型除外)