免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1577 | 回复: 4
打印 上一主题 下一主题

[请教]Core Java 2 Volume I - Fundamentals, Seventh Edition.chm中的一段话的理解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-02-16 17:43 |只看该作者 |倒序浏览
原文:

Final Instance Fields
You can define an instance field as final. Such a field must be initialized when the object is constructed. That is, it must be guaranteed that the field value has been set after the end of every constructor. Afterwards, the field may not be modified again. For example, the name field of the Employee class may be declared as final because it never changes after the object is constructed—there is no setName method.

class Employee
{
   . . .
   private final String name;
}



The final modifier is particularly useful for fields whose type is primitive or an immutable class. (A class is immutable if none of its methods ever mutate its objects. For example, the String class is immutable.) For mutable classes, the final modifier is likely to confuse the reader. For example,

private final Date hiredate;



merely means that the object reference stored in the hiredate variable doesn't get changed after the object is constructed. That does not mean that the hiredate object is constant. Any method is free to invoke the setTime mutator on the object to which hiredate refers.


请问一下:
The final modifier is particularly useful for fields whose type is primitive or an immutable class. (A class is immutable if none of its methods ever mutate its objects. For example, the String class is immutable.) For mutable classes, the final modifier is likely to confuse the reader. For example,

private final Date hiredate;



merely means that the object reference stored in the hiredate variable doesn't get changed after the object is constructed. That does not mean that the hiredate object is constant. Any method is free to invoke the setTime mutator on the object to which hiredate refers.
尤其中最后一段话
merely means that the object reference stored in the hiredate variable doesn't get changed after the object is constructed. That does not mean that the hiredate object is constant. Any method is free to invoke the setTime mutator on the object to which hiredate refers.

如何翻译,如何理解?
谁给给个例子说明一下
谢谢!!!

论坛徽章:
0
2 [报告]
发表于 2006-02-16 19:16 |只看该作者
勉强可以译作:
仅仅表示hiredate变量中存储的(也是指向的)引用是不可变(也就是说不能让hiredate变量再引用别的对象),不表明hiredate指向的引用所指向的对象是不可变的。任何方法都可以调用hiredate指向的引用的所指向的对象上的setDate()来改变该对象。


首先要明白:hiredate变量保存的是个引用,而这个引用指向一个实际的对象。

论坛徽章:
0
3 [报告]
发表于 2006-02-17 10:26 |只看该作者
那是不是这个意思呢?
final b
string a = b

setA()改变a的值
这样b的值就跟着改变了
但是不能这样用setB(),
是这个意思吗???

论坛徽章:
0
4 [报告]
发表于 2006-02-17 11:16 |只看该作者
原帖由 zhang12321 于 2006-2-17 10:26 发表
那是不是这个意思呢?
final b
string a = b

setA()改变a的值
这样b的值就跟着改变了


改变a的值,应该理解成改变引用a指向的内存地址.
之前引用a,指向的地址跟引用b指向的地址是相同的.
setA()后,引用a指向另一个地址了.但是b的一直没变啊


原帖由 zhang12321 于 2006-2-17 10:26 发表
但是不能这样用setB(),
是这个意思吗???


假如你企图在setB()里令引用b指向另一个内存地址,则是不可以的.
因为final string b= new String("xxx");(或则其他)
该句表示 对象"xxx"构建完毕后,引用b指向该对象的首地址,由于final的缘故,引用b不可以再指向别的地址,
比如b=new String("another").

虽然如此,,但是如果引用b指向的对象是可变的(mutable,string类型是不可变的),那么可以同过引用b,来改变
引用b指向的对象的内容(相当于通过引用b找到指向的内存首地址,,然后改变改连续内存单元(一个对象的表示)的内容.

ps:建议楼主熟悉Java的内存模型

论坛徽章:
0
5 [报告]
发表于 2006-02-17 11:45 |只看该作者
谢谢楼上的,问题已明白
多谢了!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP