【发布时间】:2021-02-03 15:22:17
【问题描述】:
我有一个公共类 Dog。
方法一:
public Dog getDog(Dog dog){
if(dog.size>0){
return dog;
}
return null;
}
方法二:
public Dog getNewDog(){
return new Dog();
}
最后,给方法返回值赋值:-
Dog mydog = new Dog();
Dog d2 = new Dog();
Dog c = mydog.getDog(d2); // are we assigning a reference variable back to another reference?
Dog k = mydog.getNewDog(); // Are we assigning a new dog object itself to the Dog type variable.
现在,为什么我们总是将方法中的值分配给引用变量?我在 Java 中随处可见。
【问题讨论】:
-
请尝试改进问题
-
@Aman ,我应该更具体吗?
-
当你问为什么“我们”做“总是”做 X 时,不要忘记包括你认为我们可以做的而不是 X。
标签: java object methods reference parameter-passing