Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 java 中匿名inner class 的问题?
brig





发贴: 4
积分: 0
于 2008-11-09 10:51 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
匿名inner class 中 参数必需为 final ,这点有些想不通?
inner class 的存储位置应该也是在heap中的,
匿名inner class 中 参数必需为 final ,这说明它没有传递他参数类型的指针,
而是传递的是数值,哪位高手可以告知
匿名inner class 在内存中的调用过程啊?
感激不尽?
我想了几天,还是不太理解。




作者 Re:java 中匿名inner class 的问题? [Re:brig]
zerol

Soul of New Machine



发贴: 593
积分: 61
于 2008-11-09 12:10 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
簡單地說,Java PL 與底層 bytecode 都沒有辦法獲得一個 local variable(local variable 所使用的 storage 本身)的 "reference",導致 nested class object 沒有辦法"存取" local variable 的值,勉強能做到"取"而已。底層的作法是把 local variable 的 "value" copy 一份給 nested class(對於 primitive type 甚至不需要 copy),來做出 nested class 讀取 local variable 的效果。

引自:
http://www.javaworld.com.tw/jute/post/view?bid=29&id=65988&tpg=2&ppg=1&sty=1&age=0#65988



★☆ The Truth Lies Somewhere In The Middle ☆★ 
作者 Re:java 中匿名inner class 的问题? [Re:brig]
brig





发贴: 4
积分: 0
于 2008-11-09 22:55 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
说的有太绕了,能不能通俗点啊!
有点晕 啊



作者 Re:java 中匿名inner class 的问题? [Re:brig]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-11-10 17:40 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
The anonymous inner class object may live beyond that of the method that invoked it, so you could not very well have the inner class modifying local variables in the caller that no longer exist.

That is why you may only access final local variables.

We just need to account for that limitation when using anonymous inner class. I repeat, you are not really accessing the local variables but a snapshot of them taken at the time the inner class object was instantiated. Anonymous classes are one of the bailing wire constructs in Java.

Regards,
Jiafan


JiafanZhou edited on 2008-11-14 19:28

When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923