Java 我应该返回';对象参数';从一个修改了';对象参数';在爪哇?
以下代码示例首选哪种样式 1. 或 2.Java 我应该返回';对象参数';从一个修改了';对象参数';在爪哇?,java,oop,Java,Oop,以下代码示例首选哪种样式 1. 或 2. void setErrors(对象、列表错误) { object.status=status.ERROR; ... addAll(错误); } 或 对象设置错误(对象、列表错误) { object.status=status.ERROR; ... addAll(错误); 返回对象; } 更新: 在RxJava中使用哪个更好: 3. 单个返回对象(对象) { 单回装 .doOnAccess(objectResponse->fillData(对象,obj
void setErrors(对象、列表错误)
{
object.status=status.ERROR;
...
addAll(错误);
}
或
对象设置错误(对象、列表错误)
{
object.status=status.ERROR;
...
addAll(错误);
返回对象;
}
更新:
在RxJava中使用哪个更好:
3.
单个返回对象(对象)
{
单回装
.doOnAccess(objectResponse->fillData(对象,objectResponse))
.doError(e->addErrors(对象,e))
.toCompletable()
第三个(单一的、仅仅的(对象));
}
4.
单个返回对象(对象)
{
单回装
.map(objectResponse->fillData(对象,objectResponse))
.OneErrorReturn(e->addErrors(object,e));
}
您使用的方法类型基本上是修改参数。可能有三种情况
[案例1]传递的参数是(is)原语
public void invoker(){
int x = 20;
int increment = 30;
x = getIncrementedValue(x,increment);
}
public int getIncrementedValue(int x, int inc){
return x+inc;
}
[案例2]传递的参数是不可变的
public void invoker(){
String x = "kk";
String y = nits;
x = getAppendedString(x,y);
}
public String getAppendedString(String a, String b){
a = a.append(b); // as Strings are non mutable hence this is not changed on the original passed object. The assignment of a also does not make any difference as it's local variable.
return a
}
[案例3]传递的参数是可变对象引用
public void invoker(){
Student s = new Student();
promoteStudent(s);
}
public void promoteStudent(Student s){
s.standard++;
// No need to return the object reference as same object has been modified and reference of the same is present with the invoker.
}
public class Student{
int standard = 1;
}
编辑
注释中提到的另一个用例。在链接的情况下,返回相同的对象很有用
public class Student{
String name;
int roll;
int standard;
public Student setName(String n){ this.name =n; return this;}
public Student setRoll(int n){ this.roll =n; return this;}
public Student setStandard(int n){ this.standard =n; return this}
public int getFees(){
return standard*2;
}
}
...
public void someMethod(Student s){
Int fees = s.setName("KK").setRoll(30).setStandard(10).getFees();
// Other lines of code ..
}
调用程序已经拥有可变对象的引用,因此将对同一对象的引用返回给调用程序是没有用的。在原语或不可变的情况下,应该这样做objects@nits.kk这对于方法链接并不是没有用处的,虽然
fillPlayerData
应该是Player
中的一个方法,而不是将一个作为参数。@Kayaman实际上我使用RxJava,所以我在考虑是否应该从.map
切换到.doOnNext
操作符。那么为什么你在你的问题中加入了与此无关的无用伪代码呢RxJava?@Kayaman感谢您提醒我链接,这是一种返回相同对象使调用程序更容易的情况。我已相应地编辑了我的答案。
Single<Object> returnObject(Object object)
{
return loadSingle
.doOnSuccess(objectResponse -> fillData(object, objectResponse))
.doOnError(e -> addErrors(object, e))
.toCompletable()
.andThen(Single.just(object));
}
Single<Object> returnObject(Object object)
{
return loadSingle
.map(objectResponse -> fillData(object, objectResponse))
.onErrorReturn(e -> addErrors(object, e));
}
public void invoker(){
int x = 20;
int increment = 30;
x = getIncrementedValue(x,increment);
}
public int getIncrementedValue(int x, int inc){
return x+inc;
}
public void invoker(){
String x = "kk";
String y = nits;
x = getAppendedString(x,y);
}
public String getAppendedString(String a, String b){
a = a.append(b); // as Strings are non mutable hence this is not changed on the original passed object. The assignment of a also does not make any difference as it's local variable.
return a
}
public void invoker(){
Student s = new Student();
promoteStudent(s);
}
public void promoteStudent(Student s){
s.standard++;
// No need to return the object reference as same object has been modified and reference of the same is present with the invoker.
}
public class Student{
int standard = 1;
}
public class Student{
String name;
int roll;
int standard;
public Student setName(String n){ this.name =n; return this;}
public Student setRoll(int n){ this.roll =n; return this;}
public Student setStandard(int n){ this.standard =n; return this}
public int getFees(){
return standard*2;
}
}
...
public void someMethod(Student s){
Int fees = s.setName("KK").setRoll(30).setStandard(10).getFees();
// Other lines of code ..
}