page129灰色框 (英文版page 153)
“有些人看到getName()可能会想,如果有人改动了成员变量name的话,它的返回值便也被改变了,……”
"Some people look at the getName method and suspect that it could return a different value if anyone changes the name string, but in Java, String objects are immutable. You can't change their value after they are created. After a CppClass object is created, getName always returns the same string value."
怎么能改动name呢?只有在构造函数中,name才出现在等号的左侧。
与String对象的常性(immutable?)有什么关系呢?
如果有人利用反射,改变了name的值。getName()的返回值就会相应地变化。怎么会不变呢?
改动name与CppClass.getName()的返回值
|
> 我来回应