??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲国产日本,成人国产精品免费网站,日本在线成人http://www.aygfsteel.com/ol-l/category/33935.html会笑的hM吧笑留在最后,像春雷L走在闪电之后Q却一鸣惊人!zh-cnMon, 20 Apr 2009 15:57:34 GMTMon, 20 Apr 2009 15:57:34 GMT60[转]关于六种Java异常处理E序的陋?/title><link>http://www.aygfsteel.com/ol-l/archive/2009/04/20/266636.html</link><dc:creator>ol_soft</dc:creator><author>ol_soft</author><pubDate>Mon, 20 Apr 2009 15:50:00 GMT</pubDate><guid>http://www.aygfsteel.com/ol-l/archive/2009/04/20/266636.html</guid><wfw:comment>http://www.aygfsteel.com/ol-l/comments/266636.html</wfw:comment><comments>http://www.aygfsteel.com/ol-l/archive/2009/04/20/266636.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/ol-l/comments/commentRss/266636.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/ol-l/services/trackbacks/266636.html</trackback:ping><description><![CDATA[<span style="font-family: verdana; font-size: 12px; line-height: 18px; "> <p class="MsoNormal" align="left" style="text-align: left; word-break: break-all; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; line-height: 22px; font-size: 14px; "><span style="font-family: 宋体; color: #333333; ">你觉得自己是一?/span><span lang="EN-US" style="font-family: Arial; color: #333333; ">Java</span><span style="font-family: 宋体; color: #333333; ">专家吗?是否肯定自己已经全面掌握?/span><span lang="EN-US" style="font-family: Arial; color: #333333; ">Java</span><span style="font-family: 宋体; color: #333333; ">的异常处理机Ӟ在下面这D代码中Q你能够q速找出异常处理的六个问题吗?</span><span lang="EN-US" style="font-family: Arial; color: #333333; "><o:p></o:p></span></p> <blockquote class="webkit-indent-blockquote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 40px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="color: #81888c; font-family: Arial; ">1 OutputStreamWriter out = ...</span><br /> <span style="color: #81888c; font-family: Arial; ">2 java.sql.Connection conn = ...</span><br /> <span lang="EN-US" style="font-family: Arial; color: #81888c; ">3 try { // </span><span style="font-family: 宋体; color: #81888c; ">?/span><br /> <span lang="EN-US" style="font-family: Arial; color: #81888c; ">4 </span><span style="font-family: 宋体; color: #81888c; "> </span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Statement stat = conn.createStatement();</span><br /> <span lang="EN-US" style="font-family: Arial; color: #81888c; ">5 </span><span style="font-family: 宋体; color: #81888c; "> </span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">ResultSet rs = stat.executeQuery(</span><br /> <span lang="EN-US" style="font-family: Arial; color: #81888c; ">6 </span><span style="font-family: 宋体; color: #81888c; ">  </span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">"select uid, name from user");</span><br /> <span lang="EN-US" style="font-family: Arial; color: #81888c; ">7 </span><span style="font-family: 宋体; color: #81888c; "> </span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">while (rs.next())</span><br /> <span lang="EN-US" style="font-family: Arial; color: #81888c; ">8 </span><span style="font-family: 宋体; color: #81888c; "> </span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">{</span><br /> <span lang="EN-US" style="font-family: Arial; color: #81888c; ">9 </span><span style="font-family: 宋体; color: #81888c; ">  </span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">out.println("ID</span><span style="font-family: 宋体; color: #81888c; ">Q?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">" + rs.getString("uid") // </span><span style="font-family: 宋体; color: #81888c; ">?/span><br /> <span lang="EN-US" style="font-family: Arial; color: #81888c; ">10 </span><span style="font-family: 宋体; color: #81888c; ">   </span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">"</span><span style="font-family: 宋体; color: #81888c; ">Q姓名:</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">" + rs.getString("name"));</span><br /> <span lang="EN-US" style="font-family: Arial; color: #81888c; ">11 </span><span style="font-family: 宋体; color: #81888c; "> </span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">}</span><br /> <span lang="EN-US" style="font-family: Arial; color: #81888c; ">12 </span><span style="font-family: 宋体; color: #81888c; "> </span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">conn.close(); // </span><span style="font-family: 宋体; color: #81888c; ">?/span><br /> <span lang="EN-US" style="font-family: Arial; color: #81888c; ">13 </span><span style="font-family: 宋体; color: #81888c; "> </span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">out.close();</span><br /> <span style="color: #81888c; font-family: Arial; ">14 }</span><br /> <span lang="EN-US" style="font-family: Arial; color: #81888c; ">15 catch(Exception ex) // </span><span style="font-family: 宋体; color: #81888c; ">?/span><br /> <span style="color: #81888c; font-family: Arial; ">16 {</span><br /> <span lang="EN-US" style="font-family: Arial; color: #81888c; ">17 </span><span style="font-family: 宋体; color: #81888c; "> </span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">ex.printStackTrace(); //</span><span style="font-family: 宋体; color: #81888c; ">?/span><span style="font-family: 宋体; color: #81888c; ">Q?/span><span style="font-family: 宋体; color: #81888c; ">?/span><br /> <span style="color: #81888c; font-family: Arial; ">18 }</span><br /> </blockquote> <p class="MsoNormal" align="left" style="text-align: left; word-break: break-all; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; line-height: 22px; font-size: 14px; "><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> </span><span style="font-family: 宋体; color: #81888c; ">  作ؓ一?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Java</span><span style="font-family: 宋体; color: #81888c; ">E序员,你至应该能够找Z个问题。但是,如果你不能找出全部六个问题,Ll阅L文?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  本文讨论的不?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Java</span><span style="font-family: 宋体; color: #81888c; ">异常处理的一般性原则,因ؓq些原则已经被大多数人熟知。我们要做的是分析各U可UCؓ</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">“</span><span style="font-family: 宋体; color: #81888c; ">反例</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">”</span><span style="font-family: 宋体; color: #81888c; ">Q?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">anti-pattern</span><span style="font-family: 宋体; color: #81888c; ">Q的q背优秀~码规范的常见坏习惯Q帮助读者熟悉这些典型的反面例子Q从而能够在实际工作中敏锐地察觉和避免这些问题?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  反例之一Q丢弃异?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  代码Q?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">15</span><span style="font-family: 宋体; color: #81888c; ">?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">-18</span><span style="font-family: 宋体; color: #81888c; ">行?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  q段代码捕获了异常却不作M处理Q可以算得上</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Java</span><span style="font-family: 宋体; color: #81888c; ">~程中的杀手。从问题出现的频J程度和害E度来看Q它也许可以?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">C/C++</span><span style="font-family: 宋体; color: #81888c; ">E序的一个恶名远播的问题相提q论</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">??</span><span style="font-family: 宋体; color: #81888c; ">不检查缓冲区是否已满。如果你看到了这U丢弃(而不是抛出)异常的情况,可以癑ֈ之九十九地肯定代码存在问题(在极数情况下,q段代码有存在的理由Q但最好加上完整的注释Q以免引起别解)?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  q段代码的错误在于,异常Q几乎)L意味着某些事情不对劲了Q或者说臛_发生了某些不d的事情,我们不应该对E序发出的求救信号保持沉默和无动于衷。调用一?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">printStackTrace</span><span style="font-family: 宋体; color: #81888c; ">不?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">“</span><span style="font-family: 宋体; color: #81888c; ">处理异常</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">”</span><span style="font-family: 宋体; color: #81888c; ">。不错,调用</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">printStackTrace</span><span style="font-family: 宋体; color: #81888c; ">对调试程序有帮助Q但E序调试阶段l束之后Q?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">printStackTrace</span><span style="font-family: 宋体; color: #81888c; ">׃应再在异常处理模块中担负主要责Q了?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  丢弃异常的情形非常普遍。打开</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">JDK</span><span style="font-family: 宋体; color: #81888c; ">?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">ThreadDeath</span><span style="font-family: 宋体; color: #81888c; ">cȝ文档Q可以看C面这D说明:</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">“</span><span style="font-family: 宋体; color: #81888c; ">特别圎ͼ虽然出现</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">ThreadDeath</span><span style="font-family: 宋体; color: #81888c; ">是一U?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">‘</span><span style="font-family: 宋体; color: #81888c; ">正常的情?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">’</span><span style="font-family: 宋体; color: #81888c; ">Q但</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">ThreadDeath</span><span style="font-family: 宋体; color: #81888c; ">cL</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Error</span><span style="font-family: 宋体; color: #81888c; ">而不?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Exception</span><span style="font-family: 宋体; color: #81888c; ">的子c,因ؓ许多应用会捕h有的</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Exception</span><span style="font-family: 宋体; color: #81888c; ">然后丢弃它不再理睬?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">”</span><span style="font-family: 宋体; color: #81888c; ">q段话的意思是Q虽?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">ThreadDeath</span><span style="font-family: 宋体; color: #81888c; ">代表的是一U普通的问题Q但鉴于许多应用会试图捕h有异常然后不予以适当的处理,所?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">JDK</span><span style="font-family: 宋体; color: #81888c; ">?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">ThreadDeath</span><span style="font-family: 宋体; color: #81888c; ">定义成了</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Error</span><span style="font-family: 宋体; color: #81888c; ">的子c,因ؓ</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Error</span><span style="font-family: 宋体; color: #81888c; ">cM表的是一般的应用不应该去捕获的严重问题。可见,丢弃异常q一坏习惯是如此常见Q它甚至已经影响C</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Java</span><span style="font-family: 宋体; color: #81888c; ">本n的设计?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  那么Q应该怎样Ҏ呢?主要有四个选择Q?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  </span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">1</span><span style="font-family: 宋体; color: #81888c; ">、处理异常。针对该异常采取一些行动,例如修正问题、提醒某个h或进行其他一些处理,要根据具体的情Ş定应该采取的动作。再ơ说明,调用</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">printStackTrace</span><span style="font-family: 宋体; color: #81888c; ">不上已l?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">“</span><span style="font-family: 宋体; color: #81888c; ">处理好了异常</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">”</span><span style="font-family: 宋体; color: #81888c; ">?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  </span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">2</span><span style="font-family: 宋体; color: #81888c; ">、重新抛出异常。处理异常的代码在分析异怹后,认ؓ自己不能处理它,重新抛出异常也不׃ؓ一U选择?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  </span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">3</span><span style="font-family: 宋体; color: #81888c; ">、把该异常{换成另一U异常。大多数情况下,q是指把一个低U的异常转换成应用的异常(其含义更Ҏ被用户了解的异常Q?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  </span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">4</span><span style="font-family: 宋体; color: #81888c; ">、不要捕获异常?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  l论一Q既然捕获了异常Q就要对它进行适当的处理。不要捕获异怹后又把它丢弃Q不予理睬?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  反例之二Q不指定具体的异?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  代码Q?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">15</span><span style="font-family: 宋体; color: #81888c; ">行?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  许多时候h们会被这样一U?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">“</span><span style="font-family: 宋体; color: #81888c; ">妙?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">”</span><span style="font-family: 宋体; color: #81888c; ">x吸引Q用一?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">catch</span><span style="font-family: 宋体; color: #81888c; ">语句捕获所有的异常。最常见的情形就是?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">catch(Exception ex)</span><span style="font-family: 宋体; color: #81888c; ">语句。但实际上,在绝大多数情况下Q这U做法不值得提倡。ؓ什么呢Q?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  要理解其原因Q我们必d一?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">catch</span><span style="font-family: 宋体; color: #81888c; ">语句的用途?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">catch</span><span style="font-family: 宋体; color: #81888c; ">语句表示我们预期会出现某U异常,而且希望能够处理该异常。异常类的作用就是告?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Java</span><span style="font-family: 宋体; color: #81888c; ">~译器我们想要处理的是哪一U异常。由于绝大多数异帔R直接或间接从</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">java.lang.Exception</span><span style="font-family: 宋体; color: #81888c; ">zQ?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">catch(Exception ex)</span><span style="font-family: 宋体; color: #81888c; ">q当于说我们想要处理几乎所有的异常?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  再来看看前面的代码例子。我们真正想要捕L异常是什么呢Q最明显的一个是</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">SQLException</span><span style="font-family: 宋体; color: #81888c; ">Q这?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">JDBC</span><span style="font-family: 宋体; color: #81888c; ">操作中常见的异常。另一个可能的异常?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">IOException</span><span style="font-family: 宋体; color: #81888c; ">Q因为它要操?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">OutputStreamWriter</span><span style="font-family: 宋体; color: #81888c; ">。显Ӟ在同一?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">catch</span><span style="font-family: 宋体; color: #81888c; ">块中处理q两U截然不同的异常是不合适的。如果用两个</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">catch</span><span style="font-family: 宋体; color: #81888c; ">块分别捕?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">SQLException</span><span style="font-family: 宋体; color: #81888c; ">?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">IOException</span><span style="font-family: 宋体; color: #81888c; ">p好多了。这是_</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">catch</span><span style="font-family: 宋体; color: #81888c; ">语句应当量指定具体的异常类型,而不应该指定늛范围太广?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Exception</span><span style="font-family: 宋体; color: #81888c; ">cR?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br /> <br /> </span><span style="font-family: 宋体; color: #81888c; ">  另一斚wQ除了这两个特定的异常,q有其他许多异常也可能出现。例如,如果׃某种原因Q?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">executeQuery</span><span style="font-family: 宋体; color: #81888c; ">q回?/span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">null</span><span style="font-family: 宋体; color: #81888c; ">Q该怎么办?{案是让它们l箋抛出Q即不必捕获也不必处理。实际上Q我们不能也不应该去捕获可能出现的所有异常,E序的其他地方还有捕获异常的Z</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">??</span><span style="font-family: 宋体; color: #81888c; ">直至最后由</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">JVM</span><span style="font-family: 宋体; color: #81888c; ">处理?/span></p> </span> <img src ="http://www.aygfsteel.com/ol-l/aggbug/266636.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/ol-l/" target="_blank">ol_soft</a> 2009-04-20 23:50 <a href="http://www.aygfsteel.com/ol-l/archive/2009/04/20/266636.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JDK5.0新特?-----泛型http://www.aygfsteel.com/ol-l/archive/2009/04/20/266634.htmlol_softol_softMon, 20 Apr 2009 15:49:00 GMThttp://www.aygfsteel.com/ol-l/archive/2009/04/20/266634.htmlhttp://www.aygfsteel.com/ol-l/comments/266634.htmlhttp://www.aygfsteel.com/ol-l/archive/2009/04/20/266634.html#Feedback0http://www.aygfsteel.com/ol-l/comments/commentRss/266634.htmlhttp://www.aygfsteel.com/ol-l/services/trackbacks/266634.html
泛型是Java SE 1.5的新Ҏ,泛型的本质是参数化类型,也就是说所操作的数据类型被指定Z个参数。这U参数类型可以用在类、接口和Ҏ的创ZQ分别称为泛型类、泛型接口、泛型方法?/span>
  Java语言引入泛型的好处是安全单?/span>
  在Java SE 1.5之前Q没有泛型的情况的下Q通过对类型Object的引用来实现参数?#8220;L?#8221;Q?#8220;L?#8221;带来的缺Ҏ要做昑ּ的强制类型{换,而这U{换是要求开发者对实际参数cd可以预知的情况下q行的。对于强制类型{换错误的情况Q编译器可能不提C错误,在运行的时候才出现异常Q这是一个安全隐患?/span>
  泛型的好处是在编译的时候检查类型安全,q且所有的强制转换都是自动和隐式的Q提高代码的重用率?/span>
  泛型在用中q有一些规则和限制Q?/span>
  1、泛型的cd参数只能是类cdQ包括自定义c)Q不能是单类型?/span>
  2、同一U泛型可以对应多个版本(因ؓ参数cd是不定的)Q不同版本的泛型cd例是不兼容的?/span>
  3、泛型的cd参数可以有多个?/span>
  4、泛型的参数cd可以使用extends语句Q例?lt;T extends superclass>。习惯上成ؓ“有界cd”?/span>
  5、泛型的参数cdq可以是通配W类型。例如Class<?> classType = Class.forName(java.lang.String);
  泛型q有接口、方法等{,内容很多Q需要花费一番功夫才能理解掌握ƈ熟练应用。在此给出我曄了解泛型时候写出的两个例子Q根据看的印象写的)Q实现同L功能Q一个用了泛型Q一个没有用,通过ҎQ可以很快学会泛型的应用Q学会这个基本上学会了泛?0%的内宏V?/span>
例子Q?/span>
  1. package gunct.jdk5;
  2. /**
  3. * <b>Genericity?lt;/b>
  4. * <p><b>详细说明Q?lt;/b></p>
  5. * <!-- 在此d详细说明 -->
  6. * JDK5.0新特性,泛型?/span>
  7. * <p><b>修改列表Q?lt;/b></p>
  8. * <table width="100%" cellSpacing=1 cellPadding=3 border=1>
  9. * <tr bgcolor="#CCCCFF"><td>序号</td><td>作?lt;/td><td>修改日期</td><td>修改内容</td></tr>
  10. * <!-- 在此d修改列表Q参考第一行内?-->
  11. * <tr><td>1</td><td>Oliver</td><td>2008-12-18 下午01:45:51</td><td>建立cd</td></tr>
  12. *
  13. * </table>
  14. * @version 1.0
  15. * @author Oliver
  16. * @since 1.0
  17. */
  18. public class Genericity<T>//泛型T
  19. {
  20. private T a;
  21. public void setA(T a)
  22. {
  23. this.a=a;
  24. }
  25. public T getA()
  26. {
  27. return a;
  28. }
  29. public static void main(String[] args)
  30. {
  31. //Integercd
  32. Genericity<Integer> intVal=new Genericity<Integer>();
  33. intVal.setA(new Integer(5));
  34. System.out.println(intVal.getA());
  35. //Stringcd
  36. Genericity<String> strVal= new Genericity<String>();
  37. strVal.setA("Gunct");
  38. System.out.println(strVal.getA());
  39. }
  40. }


ol_soft 2009-04-20 23:49 发表评论
]]>
Java中的transientQvolatile和strictfp关键?/title><link>http://www.aygfsteel.com/ol-l/archive/2009/04/20/266632.html</link><dc:creator>ol_soft</dc:creator><author>ol_soft</author><pubDate>Mon, 20 Apr 2009 15:45:00 GMT</pubDate><guid>http://www.aygfsteel.com/ol-l/archive/2009/04/20/266632.html</guid><wfw:comment>http://www.aygfsteel.com/ol-l/comments/266632.html</wfw:comment><comments>http://www.aygfsteel.com/ol-l/archive/2009/04/20/266632.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/ol-l/comments/commentRss/266632.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/ol-l/services/trackbacks/266632.html</trackback:ping><description><![CDATA[<span style="font-family: verdana; font-size: 12px; line-height: 18px; "> <p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; line-height: 22px; font-size: 14px; ">ava中的transientQvolatile和strictfp关键?#160;<br />     如果用transient声明一个实例变量,当对象存储时Q它的g需要维持。例如: </p> <div id="wmqeeuq" class="dp-highlighter" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 99%; overflow-x: auto; overflow-y: auto; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px; "> <div id="wmqeeuq" class="bar" style="padding-left: 45px; "> <div id="wmqeeuq" class="tools" style="padding-top: 3px; padding-right: 8px; padding-left: 10px; font: normal normal normal 9px/normal Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left-color: #6ce26c; ">Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=class%20T%20%7B%0A%20%20%20transient%20int%20a%3B%20%20%2F%2F%E4%B8%8D%E9%9C%80%E8%A6%81%E7%BB%B4%E6%8C%81%0A%20%20%20int%20b%3B%20%20%2F%2F%E9%9C%80%E8%A6%81%E7%BB%B4%E6%8C%81%0A%7D%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></div> </div> <ol class="dp-j" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; list-style-type: decimal; list-style-position: initial; list-style-image: initial; background-color: #ffffff; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #5c5c5c; "> <li style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c; background-color: #f8f8f8; color: #5c5c5c; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; line-height: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "><span id="wmqeeuq" class="keyword" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: #006699; font-weight: bold; background-color: inherit; ">class</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "> T {  </span></span></li> <li style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c; background-color: #f8f8f8; color: #5c5c5c; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; line-height: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">   <span id="wmqeeuq" class="keyword" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: #006699; font-weight: bold; background-color: inherit; ">transient</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "> </span><span id="wmqeeuq" class="keyword" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: #006699; font-weight: bold; background-color: inherit; ">int</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "> a;  </span><span id="wmqeeuq" class="comment" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: #008200; background-color: inherit; ">//不需要维?/span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">  </span></span></li> <li style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c; background-color: #f8f8f8; color: #5c5c5c; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; line-height: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">   <span id="wmqeeuq" class="keyword" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: #006699; font-weight: bold; background-color: inherit; ">int</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "> b;  </span><span id="wmqeeuq" class="comment" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: #008200; background-color: inherit; ">//需要维?/span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">  </span></span></li> <li style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c; background-color: #f8f8f8; color: #5c5c5c; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; line-height: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">}  </span></li> </ol> </div> <p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; line-height: 22px; font-size: 14px; "><br />     q里Q如果Tcȝ一个对象写入一个持久的存储区域Qa的内容不被保存,但b的将被保存?#160;<br />     volatile修饰W告诉编译器被volatile修饰的变量可以被E序的其他部分改变。在多线E程序中Q有时两个或更多的线E共享一个相同的实例变量。考虑效率问题Q每个线E可以自׃存该׃n变量的私有拷贝。实际的变量副本在不同的时候更斎ͼ如当q入synchronizedҎ时?#160;<br />     用strictfp修饰cLҎQ可以确保Q点运(以及所有切断)正如早期的Java版本那样准确。切断只影响某些操作的指数。当一个类被strictfp修饰Q所有的Ҏ自动被strictfp修饰?#160;<br />     strictfp的意思是FP-strictQ也是说精Q点的意思。在Java虚拟行Q点运时Q如果没有指定strictfp关键字时QJava的编译器以及q行环境在对点q算的表辑ּ是采取一U近g我行我素的行为来完成q些操作Q以致于得到的结果往往无法令你满意。而一旦用了strictfp来声明一个类、接口或者方法时Q那么所声明的范围内Java的编译器以及q行环境会完全依照Q点规范IEEE-754来执行。因此如果你惌你的点q算更加_Q而且不会因ؓ不同的硬件^台所执行的结果不一致的话,那就L关键字strictfp?#160;<br /> 你可以将一个类、接口以及方法声明ؓstrictfpQ但是不允许Ҏ口中的方法以及构造函数声明strictfp关键字,例如下面的代码: <br /> <br /> 1. 合法的用关键字strictfp </p> <p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; line-height: 22px; font-size: 14px; "> <div style="background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #0000FF; ">strictfp</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">interface</span><span style="color: #000000; "> A {}  <br /> </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">strictfp</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> FpDemo1 {  <br />     </span><span style="color: #0000FF; ">strictfp</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> f() {}  <br /> }</span></div> </p> <p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; line-height: 22px; font-size: 14px; "><br /> 2. 错误的用方?/p> <p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; line-height: 22px; font-size: 14px; "> <div style="background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #0000FF; ">interface</span><span style="color: #000000; "> A {    <br />     </span><span style="color: #0000FF; ">strictfp</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> f();    <br /> }    <br />     <br /> </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> FpDemo2 {    <br />     </span><span style="color: #0000FF; ">strictfp</span><span style="color: #000000; "> FpDemo2() {}    <br /> }</span></div> 一旦用了关键字strictfp来声明某个类、接口或者方法时Q那么在q个关键字所声明的范围内所有Q点运都是精的Q符合IEEE-754规范的。例如一个类被声明ؓstrictfpQ那么该cM所有的Ҏ都是strictfp的?#160;<br /> <br /> <br /> <br /> Keys: volatile <br /> <br /> 使用对象Q字D?#160;<br /> <br /> 介绍Q因为异步线E可以访问字D,所以有些优化操作是一定不能作用在字段上的。volatile有时 <br /> <br /> 可以代替synchronized?#160;<br /> <br /> <br /> <br /> <br /> KeysQtransient <br /> <br />   使用对象Q字D?#160;<br /> <br />   介绍Q字D不是对象持久状态的一部分Q不应该把字D和对象一起串赗?#160;</p> </span> <img src ="http://www.aygfsteel.com/ol-l/aggbug/266632.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/ol-l/" target="_blank">ol_soft</a> 2009-04-20 23:45 <a href="http://www.aygfsteel.com/ol-l/archive/2009/04/20/266632.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>