class DogOwner {
Dog dog;
DogOwner(Dog dog) {
this.dog = dog;
}
}
class Dog {
int age;
Dog(int age) {
this.age = age;
}
}
DogOwner peter = new DogOwner(new Dog(2));
Dog max = peter.dog;
max.age = 3;
System.out.println(peter.dog.age); // 3
我如何在不引用所拥有的情况下max
从中检索?换句话说,我希望能够在不改变' 的情况下将' 年龄设置为 3 。peter
max
Dog
peter
max
peter
Dog