??xml version="1.0" encoding="utf-8" standalone="yes"?>精品一区二区三区影院在线午夜 ,资源视频在线播放免费,成人免费视频ahttp://www.aygfsteel.com/airong/category/40538.html春风不相识,何事入罗帏?zh-cnWed, 29 Jul 2009 12:06:57 GMTWed, 29 Jul 2009 12:06:57 GMT60Java需要引入闭包吗Q?/title><link>http://www.aygfsteel.com/airong/articles/288830.html</link><dc:creator>?sh)子?/dc:creator><author>?sh)子?/author><pubDate>Tue, 28 Jul 2009 18:33:00 GMT</pubDate><guid>http://www.aygfsteel.com/airong/articles/288830.html</guid><wfw:comment>http://www.aygfsteel.com/airong/comments/288830.html</wfw:comment><comments>http://www.aygfsteel.com/airong/articles/288830.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/airong/comments/commentRss/288830.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/airong/services/trackbacks/288830.html</trackback:ping><description><![CDATA[今天在javaeye论坛里看到的帖子Q认真地从头看到,寚w包有了进一步的认识。感谢原帖楼d所有回帖的朋友?br /> <br /> 原帖地址Q?a target="_blank" >http://www.javaeye.com/topic/412451</a><br /> 主题Q再论闭?br /> 作者:(x)步行?br /> 寚w包感兴趣的朋友可以移步去看看?br /> <br /> 首先Q给出闭包的定义Q定义说法比较多Q本人偷懒,q接从原帖里摘取了。)Q?br /> <br /> <p style="margin: 0px; padding: 0px;"><strong style="font-weight: bold;"><span style="color: rgb(0, 0, 255);">闭包</span> <span style="color: rgb(255, 0, 0);"> </span> </strong>是可以包?span style="color: rgb(0, 0, 255);">自由Q未l定Q变?/span> 的代码块Q这些变量不是在q个代码块或者Q何全局上下文中定义的,而是在定义代码块的环境中定义?#8220;闭包” 一词来源于以下两者的l合Q要执行的代码块Q由于自由变量的存在Q相兛_量引用没有释放)和ؓ(f)自由变量提供l定的计环境(作用域)?/p> <p style="margin: 0px; padding: 0px;"><br /> </p> <p style="margin: 0px; padding: 0px;">    可能上面的定义有Ҏ(gu)涩,下面看一?span style="color: rgb(0, 0, 255);">《Python 核心~程?/span> ?nbsp;<strong style="font-weight: bold;"><span style="color: rgb(0, 0, 255);">闭包 <span style="font-weight: normal;"><span style="color: rgb(0, 0, 0);">的解释?/span></span></span></strong></p> <p style="margin: 0px; padding: 0px;"><strong style="font-weight: bold;"><span style="color: rgb(0, 0, 255);"><span style="font-weight: normal;"><span style="color: rgb(0, 0, 0);"><br /> </span></span></span></strong></p> <p style="margin: 0px; padding: 0px;"><strong style="font-weight: bold;"><span><span style="font-weight: normal;">    如果在一个内部函数里Q对在外部作用域Q但不是在全局作用域)的变量进行引用,那么内部函数p定义?strong style="font-weight: bold;"><span style="color: rgb(0, 0, 255);">闭包</span> </strong>。定义在外部函数内的但由内部函数引用或者用的变量被称?span style="color: rgb(0, 0, 255);">自由变量</span> ?/span></span></strong></p> <p style="margin: 0px; padding: 0px;"><strong style="font-weight: bold;"></strong></p> <p style="margin: 0px; padding: 0px;"><strong style="font-weight: bold;"></strong></p> <p style="margin: 0px; padding: 0px;"><strong style="font-weight: bold;"></strong></p> <p style="margin: 0px; padding: 0px;"> </p> 原帖?#8220;liusong1111”回帖值得思考:(x)<br /> <strong>[<em>closure作ؓ(f)一{公民,可以被一个普通的变量引用、传递。在ruby里意味着它是Proc的一个对象(实例Q,自然可以调用Proc的方法(比如 callQ。以java的视角,cL用来l织数据的,不是描述行ؓ(f)。而对于rubyQ行Z是数据,也可以用cLqͼcM是对象,cȝc还是对象,q才? 真正的Everything is Object。楼djava和java所谓的OO的媄(jing)响太深,评判q些问题Ӟ应该眼光往外多看看Q避免思维定势。尤其应该深入学?fn)一门带闭包的语aQ? 看看它到底是怎么用的?/em>]</strong><br /> <br /> 首先Q我同意可以用OO模拟闭包的观点,q已l由原帖作?#8220;步行?#8221;l出详细的例子了。但是这势必在一定程度上?x)增加代码的复杂度和臃肿度。我惻I大部分时候大家更希望用更z的Ҏ(gu)d成同L(fng)功能吧?br /> <br /> 另外Q对于闭包会(x)破坏OO的结构的争论Q我更愿意从q义的角度来理解OO的定义。正如上面引用中所_(d)“<strong><em>以java的视角,cL用来l织数据的,不是描述行ؓ(f)。而对于rubyQ行Z是数据,也可以用cLqͼcM是对象,cȝc还是对?/em></strong>”。如果采用闭包,那么q个内部函数中可以定义局部变量,同时它本w就是行为,此时h属性和行ؓ(f)的闭包,何尝不可以看做是OO的呢Q?br /> 个h的观Ҏ(gu)如果能达C下两条,那么我支持Java中引入闭包:(x)<br /> <br /> 1Q引入闭包能够在保证E序的可L的基础上,化程序编写的复杂度,降低代码的臃肿度?br /> 2Q引入闭包至能够保证在大部分情况下Q程序的性能能够保持或者得到提升。(q里排出了极限情况)<br /> <br /> 本h寚w包的了解q远q不够,计划有空闲的时候,认真地学?fn)下闭包的知识和相关的应用?br /> <br /> Ƣ迎讨论?br /> <br /> <img src ="http://www.aygfsteel.com/airong/aggbug/288830.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/airong/" target="_blank">?sh)子?/a> 2009-07-29 02:33 <a href="http://www.aygfsteel.com/airong/articles/288830.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]深入理解java多态性及(qing)个h的一Ҏ(gu)?/title><link>http://www.aygfsteel.com/airong/articles/286340.html</link><dc:creator>?sh)子?/dc:creator><author>?sh)子?/author><pubDate>Fri, 10 Jul 2009 22:39:00 GMT</pubDate><guid>http://www.aygfsteel.com/airong/articles/286340.html</guid><wfw:comment>http://www.aygfsteel.com/airong/comments/286340.html</wfw:comment><comments>http://www.aygfsteel.com/airong/articles/286340.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/airong/comments/commentRss/286340.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/airong/services/trackbacks/286340.html</trackback:ping><description><![CDATA[原作者thinkGhosterQ链?a >http://blog.csdn.net/thinkGhoster/archive/2008/04/19/2307001.aspx</a><br />对原文进行了适当的调_(d)更适合阅读?br /><br /><b>注:(x)</b>本hd的一Ҏ(gu)考在文章最后?br /><p><strong><font style="background-color: rgb(255, 255, 255);" size="4" color="#339966">Q一Q相关类</font></strong></p><div style="border: 0.5pt solid windowtext; padding: 4px 5.4pt; background: rgb(230, 230, 230) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; width: 95%;"><div><img id="_8_178_Open_Image" onclick="this.style.display='none'; document.getElementById('_8_178_Open_Text').style.display='none'; document.getElementById('_8_178_Closed_Image').style.display='inline'; document.getElementById('_8_178_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="_8_178_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('_8_178_Closed_Text').style.display='none'; document.getElementById('_8_178_Open_Image').style.display='inline'; document.getElementById('_8_178_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align="top" /><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> A </span><span id="_8_178_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">...</span><span id="_8_178_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img id="_44_91_Open_Image" onclick="this.style.display='none'; document.getElementById('_44_91_Open_Text').style.display='none'; document.getElementById('_44_91_Closed_Image').style.display='inline'; document.getElementById('_44_91_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="_44_91_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('_44_91_Closed_Text').style.display='none'; document.getElementById('_44_91_Open_Image').style.display='inline'; document.getElementById('_44_91_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align="top" />         </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String show(D obj)</span><span id="_44_91_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">...</span><span id="_44_91_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" />                </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">A and D</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />         }</span></span><span style="color: rgb(0, 0, 0);"> <br /><img id="_128_175_Open_Image" onclick="this.style.display='none'; document.getElementById('_128_175_Open_Text').style.display='none'; document.getElementById('_128_175_Closed_Image').style.display='inline'; document.getElementById('_128_175_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="_128_175_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('_128_175_Closed_Text').style.display='none'; document.getElementById('_128_175_Open_Image').style.display='inline'; document.getElementById('_128_175_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align="top" />         </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String show(A obj)</span><span id="_128_175_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">...</span><span id="_128_175_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" />                </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">A and A</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />         }</span></span><span style="color: rgb(0, 0, 0);"> <br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span><span style="color: rgb(0, 0, 0);"> <br /><img id="_198_367_Open_Image" onclick="this.style.display='none'; document.getElementById('_198_367_Open_Text').style.display='none'; document.getElementById('_198_367_Closed_Image').style.display='inline'; document.getElementById('_198_367_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="_198_367_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('_198_367_Closed_Text').style.display='none'; document.getElementById('_198_367_Open_Image').style.display='inline'; document.getElementById('_198_367_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align="top" /></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> B </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> A</span><span id="_198_367_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">...</span><span id="_198_367_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img id="_234_281_Open_Image" onclick="this.style.display='none'; document.getElementById('_234_281_Open_Text').style.display='none'; document.getElementById('_234_281_Closed_Image').style.display='inline'; document.getElementById('_234_281_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="_234_281_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('_234_281_Closed_Text').style.display='none'; document.getElementById('_234_281_Open_Image').style.display='inline'; document.getElementById('_234_281_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align="top" />         </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String show(B obj)</span><span id="_234_281_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">...</span><span id="_234_281_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" />                </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">B and B</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />         }</span></span><span style="color: rgb(0, 0, 0);"><br /><img id="_317_364_Open_Image" onclick="this.style.display='none'; document.getElementById('_317_364_Open_Text').style.display='none'; document.getElementById('_317_364_Closed_Image').style.display='inline'; document.getElementById('_317_364_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="_317_364_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('_317_364_Closed_Text').style.display='none'; document.getElementById('_317_364_Open_Image').style.display='inline'; document.getElementById('_317_364_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align="top" />         </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String show(A obj)</span><span id="_317_364_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">...</span><span id="_317_364_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" />                </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">B and A</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />         }</span></span><span style="color: rgb(0, 0, 0);"> <br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span><span style="color: rgb(0, 0, 0);"><br /><img id="_386_387_Open_Image" onclick="this.style.display='none'; document.getElementById('_386_387_Open_Text').style.display='none'; document.getElementById('_386_387_Closed_Image').style.display='inline'; document.getElementById('_386_387_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="_386_387_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('_386_387_Closed_Text').style.display='none'; document.getElementById('_386_387_Open_Image').style.display='inline'; document.getElementById('_386_387_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align="top" /></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> C </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> B</span><span id="_386_387_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">...</span><span id="_386_387_Open_Text"><span style="color: rgb(0, 0, 0);">{}</span></span><span style="color: rgb(0, 0, 0);"> <br /><img id="_407_408_Open_Image" onclick="this.style.display='none'; document.getElementById('_407_408_Open_Text').style.display='none'; document.getElementById('_407_408_Closed_Image').style.display='inline'; document.getElementById('_407_408_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="_407_408_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('_407_408_Closed_Text').style.display='none'; document.getElementById('_407_408_Open_Image').style.display='inline'; document.getElementById('_407_408_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align="top" /></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> D </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> B</span><span id="_407_408_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">...</span><span id="_407_408_Open_Text"><span style="color: rgb(0, 0, 0);">{}</span></span><span style="color: rgb(0, 0, 0);"> </span></div></div><p><font size="4" color="#339966"><strong>Q二Q问题:(x)以下输出l果是什么?</strong></font></p><div style="border: 0.5pt solid windowtext; padding: 4px 5.4pt; background: rgb(230, 230, 230) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; width: 95%;"><div><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />        <span style="color: rgb(0, 0, 0);">A a1 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> A();<br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />        A a2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> B();<br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />        B b = </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> B();<br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />        C c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> C(); <br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />        D d </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> D(); <br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />        </span><span style="color: rgb(0, 0, 0);">System.out.println(a1.show(b));   ?br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />        System.out.println(a1.show(c));   ?br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />        System.out.println(a1.show(d));   ?br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />        System.out.println(a2.show(b));   ?br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />        System.out.println(a2.show(c));   ?br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />        System.out.println(a2.show(d));   ?br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />        System.out.println(b.show(b));     ?br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />        System.out.println(b.show(c));     ?br /><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" />        System.out.println(b.show(d));     ⑨   ?/span></div></div><p><strong><font size="4" color="#339966">Q三Q答?/font></strong></p><p>              ① ?A and A<br />              ② ?A and A<br />              ③ ?A and D<br />              ④ ?B and A<br />              ⑤ ?B and A<br />              ⑥ ?A and D<br />              ⑦ ?B and B<br />              ⑧ ?B and B<br />              ⑨ ?A and D</p><p><font size="4" color="#339966"><strong>Q四Q分?/strong></font></p><p>        <font size="3">①②③比较好理解Q一般不?x)出错。④⑤就有点p涂了,Z么输出的不是"B and B”呢Q!Q先来回一下多态性?/font></p><p><font size="3">        q行时多态性是面向对象E序设计代码重用的一个最强大机制Q动态性的概念也可以被说成“一个接口,多个Ҏ(gu)”。Java实现q行时多态性的基础是动态方法调度,它是一U在q行时而不是在~译期调用重载方法的机制?/font></p><p><font size="3">        Ҏ(gu)的重写Overriding和重载Overloading是Java多态性的不同表现?/font></p><p><font size="3">       - <i><b>重写(Overriding)</b></i> 是父cM子类之间多态性的一U表现?/font><font size="3">如果在子cM定义某方法与其父cL相同的名U和参数Q我们说该方法被重写 (Overriding)。子cȝ对象使用q个Ҏ(gu)Ӟ调用子cM的定义,对它而言Q父cM的定义如同被“屏蔽”了?/font></p><p><font size="3">       - <i><b>重蝲(Overloading)</b></i> 是一个类中多态性的一U表现。如果在一个类中定义了多个同名的方法,它们或有不同的参C数或有不同的参数cdQ则UCؓ(f)Ҏ(gu)的重?Overloading)。Overloaded的方法是可以改变q回值的cd?/font></p><p><font size="3">        <font color="#0000ff"><strong><em>当超cd象引用变量引用子cd象时Q被引用对象的类型而不是引用变量的cd军_了调用谁的成员方法,但是q个被调用的Ҏ(gu)必须是在类中定义过的,也就是说被子c覆盖的Ҏ(gu)?/em></strong></font> Q但是如果强制把类转换成子cȝ话,可以调用子cM新添加而超cL有的Ҏ(gu)了。)</font></p><p><font size="3">        好了Q先温习(fn)到这里,a归正传!实际上这里涉?qing)方法调用的优先问? Q优先由高C依次为:(x)</font></p><p><font size="3">       this.show(O), <br /></font></p><p><font size="3">       super.show(O)Q?br /></font></p><p><font size="3">       this.show((super)O),<br /></font></p><p><font size="3">       super.show((super)O)。让我们来看看它是怎么工作的?/font></p><p><font size="3">        比如④,a2.show(b)Qa2是一个引用变量,cd为AQ则this为a2Qb是B的一个实例,于是它到cA里面找show(B obj)Ҏ(gu)Q没有找刎ͼ于是到A的super(类)找,而A没有类Q因此{到第三优先this.show((super)O)Qthis仍然? a2Q这里O为BQ?super)O?super)B即AQ因此它到类A里面找show(A obj)的方法,cA有这个方法,但是׃a2引用的是cB的一个对象,B覆盖了A的show(A obj)Ҏ(gu)Q因此最l锁定到cB的show(A obj)Q输Zؓ(f)"B and A”?/font></p><p><font size="3">        再比如⑧Qb.show(c)Qb是一个引用变量,cd为BQ则this为bQc是C的一个实例,于是它到cB找show(C obj)Ҏ(gu)Q没有找刎ͼ转而到B的超cA里面找,A里面也没有,因此也{到第三优先this.show((super)O)Qthis为bQO为CQ? (super)O?super)C即BQ因此它到B里面找show(B obj)Ҏ(gu)Q找CQ由于b引用的是cB的一个对象,因此直接锁定到类B的show(B obj)Q输Zؓ(f)"B and B”?/font></p><p><font size="3">        按照上面的方法,可以正确得到其他的结果?/font></p><p><font size="3">        问题q要l箋Q现在我们再来看上面的分析过E是怎么体现色字体那句话的内늚。它_(d)(x)当超cd象引用变量引用子cd象时Q被引用对象的类型而不是引? 变量的类型决定了调用谁的成员Ҏ(gu)Q但是这个被调用的方法必L在超cM定义q的Q也是说被子类覆盖的方法。还是拿a2.show(b)来说吧?/font></p><p><font size="3">        a2是一个引用变量,cd为AQ它引用的是B的一个对象,因此q句话的意思是由B来决定调用的是哪个方法。因此应该调用B的show(B obj)从而输?B and B”才寏V但是ؓ(f)什么跟前面的分析得到的l果不相W呢Q问题在于我们不要忽略了蓝色字体的后半部分,那里特别指明Q这个被调用的方法必L在超cM定义q的Q也是被子c覆盖的Ҏ(gu)。B里面的show(B obj)在超cA中有定义吗?没有Q那更谈不上被覆盖了。实际上q句话隐藏了一条信息:(x)它仍然是按照Ҏ(gu)调用的优先来确定的。它在类A中找C show(A obj)Q如果子cB没有覆盖show(A obj)Ҏ(gu)Q那么它?yu)p用A的show(A obj)Q由于Bl承AQ虽然没有覆盖这个方法,但从类A那里l承了这个方法,从某U意义上_(d)q是由B定调用的方法,只是Ҏ(gu)是在A中实现而已Q; 现在子类B覆盖了show(A obj)Q因此它最l锁定到B的show(A obj)。这是那句话的意义所在?/font></p><p><font size="3"><br /></font></p><p><font size="3" face="Arial"><u><b>个hd的一Ҏ(gu)考:(x)</b></u></font></p><p><font size="3" face="Arial">W?个问题值得思考,输出 a2.show(c) Q答案是 B and A</font></p><p><font size="3" face="Arial"><b>分析Q?/b><br /></font></p><p><font size="3" face="Arial">a2是类A的引用变量,引用的是cB的一个实例对象。c是类C的一个实例?/font></p><p><font size="3" face="Arial"> 1Qthis.show(c)Q在A中找show(C obj)的方法,没有扑ֈQ进行第二步?<br /></font></p><p><font size="3" face="Arial">2Qsuper.show(c)QA没有类Q进行第三步?<br /></font></p><p><font size="3" face="Arial">3Qthis.show(super(c)), super(c)是BQA中是没有show(B obj)Ҏ(gu)的,但是q时候会(x)l箋往上找Q找B的父c,super(B)是AQ在A中找show(A obj), 扑ֈ了?<br /></font></p><p><font size="3" face="Arial">4Qa2引用的是cB的一个对象,由B来决定调用哪个方法,因ؓ(f)cB中覆盖了A的show(A obj)Ҏ(gu)Q因此最l用cB中的show(A obj)Ҏ(gu)。?/font></p><p><font size="3" face="Arial">所以结论是q里super(O)是采用了</font><font size="3" color="#336600" face="Arial"><b>逐层往上寻?/b></font><font size="3" face="Arial">的方式,具体来说Q父cL不到对应的showҎ(gu)Q就扄cȝ父类Q如果还没有Ql往上找Q直到找到根基类?/font></p><p><font size="3" face="Arial">Z验证作者原文中蓝色字体D|字的正确性,我测试了输出a1.show(c)Q结果和预想的一P是A and A. <br /></font></p><p><font size="3" face="Arial">q里Qa1.show(c)和a2.show(c)的区别在于持有的引用对象的不同,a1是持有了Acȝ一个实例对象,而a2是持有了Bcȝ一个实例对象,所以当都找CAcM?span id="_8_178_Open_Text"><span style="color: rgb(0, 0, 0);">show(A obj)Ӟ引用的对象的cd军_了该调用哪个成员的方法,前提是这个被调用的方法必被子类覆盖了。所?/span></span>a1.show(c)输出的是A and AQ?<span id="_8_178_Open_Text"><span style="color: rgb(0, 0, 0);"></span></span>a2.show(c)输出的是 B and A.</font><font size="3"><font color="#0000ff"><strong><em></em></strong></font></font></p><font size="4" color="#339966"><strong></strong></font><img src ="http://www.aygfsteel.com/airong/aggbug/286340.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/airong/" target="_blank">?sh)子?/a> 2009-07-11 06:39 <a href="http://www.aygfsteel.com/airong/articles/286340.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <a href="http://www.aygfsteel.com/" title="狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频">狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频</a> </div> </footer> վ֩ģ壺 <a href="http://" target="_blank">կ</a>| <a href="http://" target="_blank">Դ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ˮ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ɽ</a>| <a href="http://" target="_blank">㰲</a>| <a href="http://" target="_blank">޻</a>| <a href="http://" target="_blank">ұ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">־</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ɳ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">彧</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ų</a>| <a href="http://" target="_blank">ɳ</a>| <a href="http://" target="_blank">ݳ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ӧ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">㶫ʡ</a>| <a href="http://" target="_blank">ר</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">º</a>| <a href="http://" target="_blank">ξ</a>| <a href="http://" target="_blank">γ</a>| <a href="http://" target="_blank">ں</a>| <a href="http://" target="_blank">Ƿ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">÷ӿ</a>| <a href="http://" target="_blank">ɽ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>