﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-I believe-随笔分类-Java笔记</title><link>http://www.cppblog.com/luyulaile/category/15751.html</link><description>I  can</description><language>zh-cn</language><lastBuildDate>Tue, 27 Nov 2012 12:52:28 GMT</lastBuildDate><pubDate>Tue, 27 Nov 2012 12:52:28 GMT</pubDate><ttl>60</ttl><item><title>Java 字符串比较</title><link>http://www.cppblog.com/luyulaile/archive/2012/11/22/195517.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Wed, 21 Nov 2012 16:44:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2012/11/22/195517.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/195517.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2012/11/22/195517.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/195517.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/195517.html</trackback:ping><description><![CDATA[原文：<a href="http://www.cnblogs.com/tonyqus/archive/2004/12/07/73710.html">http://www.cnblogs.com/tonyqus/archive/2004/12/07/73710.html</a>&nbsp;<span style="color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; "></span><br /><span style="color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; "><br /><br />熟悉C++的人对于两个字符串比较的代码一定很了解：&nbsp;</span><br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><span style="color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; ">(string1==string2)&nbsp;</span><br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><span style="color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; ">但在java中，这个代码即使在两个字符串完全相同的情况下也会返回false&nbsp;</span><br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><span style="color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; ">Java中必须使用string1.equals(string2)来进行判断&nbsp;</span><br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><strong style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; ">补充</strong>&nbsp;<br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><span style="color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; ">如果：&nbsp;</span><br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><span style="color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; ">string s1=new String("Hello");&nbsp;</span><br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><span style="color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; ">string s2=new String("Hello");&nbsp;</span><br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><span style="color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; ">则(s1==s2)=false&nbsp;</span><br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><span style="color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; ">如果：&nbsp;</span><br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><span style="color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; ">string s1="Hello";&nbsp;</span><br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><span style="color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; ">string s2="Hello";&nbsp;</span><br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><span style="color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; ">则(s1==s2)=true;&nbsp;</span><br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><span style="color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; ">因为他们指向的同一个对象。&nbsp;</span><br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><br style="margin: 0px; padding: 0px; color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; " /><span style="color: #393939; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; background-color: #faf7ef; ">如果把其他变量的值赋给s1和s2，即使内容相同，由于不是指向同一个对象，也会返回false。所以建议使用equals()，因为equals比较的才是真正的内容&nbsp;</span><img src ="http://www.cppblog.com/luyulaile/aggbug/195517.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2012-11-22 00:44 <a href="http://www.cppblog.com/luyulaile/archive/2012/11/22/195517.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java 中的 Hashmap serialization 非常耗费空间 Java线程</title><link>http://www.cppblog.com/luyulaile/archive/2012/04/06/170227.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Thu, 05 Apr 2012 20:32:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2012/04/06/170227.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/170227.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2012/04/06/170227.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/170227.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/170227.html</trackback:ping><description><![CDATA[不需要序列化的变量请用transient 注释<br /><div><br />调试Java程序的时候，一定要注意Java线程，何时挂起，是否会终止，能否需要新建，start 还是resume.</div><img src ="http://www.cppblog.com/luyulaile/aggbug/170227.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2012-04-06 04:32 <a href="http://www.cppblog.com/luyulaile/archive/2012/04/06/170227.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Eclipse Fatjar包的调试</title><link>http://www.cppblog.com/luyulaile/archive/2012/04/05/170117.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Thu, 05 Apr 2012 01:14:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2012/04/05/170117.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/170117.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2012/04/05/170117.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/170117.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/170117.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; background-color: #ffffff; ">使用命令行 ：<br />到其路径下运行 java -jar xxx.jar -pause 运行这个文件，这时进行操作就可以看到所报错误</span><img src ="http://www.cppblog.com/luyulaile/aggbug/170117.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2012-04-05 09:14 <a href="http://www.cppblog.com/luyulaile/archive/2012/04/05/170117.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>事件响应程序写成异步线程</title><link>http://www.cppblog.com/luyulaile/archive/2012/04/04/169989.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Tue, 03 Apr 2012 20:36:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2012/04/04/169989.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/169989.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2012/04/04/169989.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/169989.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/169989.html</trackback:ping><description><![CDATA[基本的需求是： 按下Btn Begin, 开始处理，同时监听Btn Pause,一旦Pause就Pause，如果没有Pause就执行直到结束<br />我们需要将事件响应程序写成Thread，实现异步监听。<br />在嵌入类中调用this,需要类名+this.<br />代码1：响应部分<br /><div style="font-size: 13px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; background-color: #eeeeee; "><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">package</span><span style="color: #000000; ">&nbsp;firstversion;<br /><br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;ThreadForUpdate&nbsp;</span><span style="color: #0000FF; ">extends</span><span style="color: #000000; ">&nbsp;Thread{<br />&nbsp;&nbsp;&nbsp;&nbsp;ControlPanel&nbsp;cp;<br />&nbsp;&nbsp;&nbsp;&nbsp;Simulator&nbsp;r;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;ThreadForUpdate(ControlPanel&nbsp;cp,Simulator&nbsp;r)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.cp</span><span style="color: #000000; ">=</span><span style="color: #000000; ">cp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.r</span><span style="color: #000000; ">=</span><span style="color: #000000; ">r;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;run()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">while</span><span style="color: #000000; ">(cp.flag</span><span style="color: #000000; ">==</span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.DisplaySolutionRecursive();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cp.textFieldStability.setText(r.node.stabilityState.stability.toString());<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cp.textFieldDirection.setText(r.node.controlledState.direction.toString());<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cp.textFieldDifficulty.setText(r.node.controlledState.difficulty.toString());<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cp.textFieldActionExecuting.setText(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">+</span><span style="color: #000000; ">r.node.stabilityState.actor</span><span style="color: #000000; ">+</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,</span><span style="color: #000000; ">"</span><span style="color: #000000; ">+</span><span style="color: #000000; ">r.node.stabilityState.actionBeExecuting</span><span style="color: #000000; ">+</span><span style="color: #000000; ">"</span><span style="color: #000000; ">)</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cp.textFieldStability.setText(r.node.stabilityState.stability.toString());<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cp.textFieldX.setText(</span><span style="color: #000000; ">""</span><span style="color: #000000; ">+</span><span style="color: #000000; ">r.node.controlledState.x);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cp.textFieldY.setText(</span><span style="color: #000000; ">""</span><span style="color: #000000; ">+</span><span style="color: #000000; ">r.node.controlledState.y);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cp.textFieldTraffic.setText(r.node.traffic.toString());<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(r.node.isTerminal</span><span style="color: #000000; ">==</span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cp.textFieldNextAction.setText(</span><span style="color: #000000; ">""</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">else</span><span style="color: #000000; "><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cp.textFieldNextAction.setText(r.node.bestAction.toString());<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cp.textFieldEngagement.setText(r.node.engagement.toString());<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">try</span><span style="color: #000000; ">&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.sleep(</span><span style="color: #000000; ">200</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000FF; ">catch</span><span style="color: #000000; ">&nbsp;(InterruptedException&nbsp;e)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />}<br /></span></div>代码2：事件响应<br /><div style="font-size: 13px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; background-color: #eeeeee; "><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">btnBegin&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&nbsp;JButton(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">Begin</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;btnBegin.setEnabled(</span><span style="color: #0000FF; ">false</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">final</span><span style="color: #000000; ">&nbsp;ThreadForUpdate&nbsp;threadUpdate</span><span style="color: #000000; ">=</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&nbsp;ThreadForUpdate(ControlPanel.</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">,r);;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;btnBegin.addActionListener(</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&nbsp;ActionListener()&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;actionPerformed(ActionEvent&nbsp;arg0)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;threadUpdate.start();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});</span></div><br /><br /><br /><br /><br /><img src ="http://www.cppblog.com/luyulaile/aggbug/169989.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2012-04-04 04:36 <a href="http://www.cppblog.com/luyulaile/archive/2012/04/04/169989.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]Java反射经典实例 Java Reflection Cookbook</title><link>http://www.cppblog.com/luyulaile/archive/2012/02/16/165718.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Thu, 16 Feb 2012 01:19:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2012/02/16/165718.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/165718.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2012/02/16/165718.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/165718.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/165718.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Java提供了一套机制来动态执行方法和构造方法，以及数组操作等，这套机制就叫&#8212;&#8212;反射。反射机制是如今很多流行框架的实现基础，其中包括Spring、Hibernate等。原理性的问题不是本文的重点，接下来让我们在实例中学习这套精彩的机制。1. 得到某个对象的属性public&nbsp;Object&nbsp;getProperty(Object&nbsp;owner,&nbs...&nbsp;&nbsp;<a href='http://www.cppblog.com/luyulaile/archive/2012/02/16/165718.html'>阅读全文</a><img src ="http://www.cppblog.com/luyulaile/aggbug/165718.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2012-02-16 09:19 <a href="http://www.cppblog.com/luyulaile/archive/2012/02/16/165718.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java 内部类 反射的实现</title><link>http://www.cppblog.com/luyulaile/archive/2012/02/16/165714.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Thu, 16 Feb 2012 01:17:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2012/02/16/165714.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/165714.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2012/02/16/165714.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/165714.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/165714.html</trackback:ping><description><![CDATA[<div style="background-color:#eeeeee;font-size:13px;border:1px solid #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; ">package</span><span style="color: #000000; ">&nbsp;homework1;<br /><br /></span><span style="color: #0000FF; ">import</span><span style="color: #000000; ">&nbsp;java.lang.reflect.Constructor;<br /></span><span style="color: #0000FF; ">import</span><span style="color: #000000; ">&nbsp;java.lang.reflect.InvocationTargetException;<br /></span><span style="color: #0000FF; ">import</span><span style="color: #000000; ">&nbsp;java.util.Vector;<br /><br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;BayesianNetwork&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">static</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">final</span><span style="color: #000000; ">&nbsp;String&nbsp;Less</span><span style="color: #000000; ">=</span><span style="color: #000000; ">"</span><span style="color: #000000; ">&lt;45</span><span style="color: #000000; ">"</span><span style="color: #000000; ">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">static</span><span style="color: #000000; ">&nbsp;&nbsp;</span><span style="color: #0000FF; ">final</span><span style="color: #000000; ">&nbsp;String&nbsp;Middle</span><span style="color: #000000; ">=</span><span style="color: #000000; ">"</span><span style="color: #000000; ">45-55</span><span style="color: #000000; ">"</span><span style="color: #000000; ">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">static</span><span style="color: #000000; ">&nbsp;&nbsp;</span><span style="color: #0000FF; ">final</span><span style="color: #000000; ">&nbsp;String&nbsp;More</span><span style="color: #000000; ">=</span><span style="color: #000000; ">"</span><span style="color: #000000; ">&gt;=55</span><span style="color: #000000; ">"</span><span style="color: #000000; ">;<br /></span><span style="color: #0000FF; ">enum</span><span style="color: #000000; ">&nbsp;A{Less,Middle,More};<br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;NodeA&nbsp;</span><span style="color: #0000FF; ">extends</span><span style="color: #000000; ">&nbsp;Node{<br />&nbsp;&nbsp;&nbsp;&nbsp;A&nbsp;value;<br />}<br /></span><span style="color: #0000FF; ">enum</span><span style="color: #000000; ">&nbsp;G{Female,Male};<br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;NodeG&nbsp;</span><span style="color: #0000FF; ">extends</span><span style="color: #000000; ">&nbsp;Node{<br />&nbsp;&nbsp;&nbsp;&nbsp;G&nbsp;value;<br />}<br /></span><span style="color: #0000FF; ">enum</span><span style="color: #000000; ">&nbsp;CP{Typical,Atypical,NonAnginal,None};<br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;NodeCP&nbsp;</span><span style="color: #0000FF; ">extends</span><span style="color: #000000; ">&nbsp;Node{<br />&nbsp;&nbsp;&nbsp;&nbsp;CP&nbsp;value;<br />}<br /></span><span style="color: #0000FF; ">enum</span><span style="color: #000000; ">&nbsp;BP{Low,High};<br /></span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;NodeBP&nbsp;</span><span style="color: #0000FF; ">extends</span><span style="color: #000000; ">&nbsp;Node{<br />&nbsp;&nbsp;&nbsp;&nbsp;BP&nbsp;value;<br />}<br /></span><span style="color: #0000FF; ">enum</span><span style="color: #000000; ">&nbsp;CH{Low,High};<br /></span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;NodeCH&nbsp;</span><span style="color: #0000FF; ">extends</span><span style="color: #000000; ">&nbsp;Node{<br />&nbsp;&nbsp;&nbsp;&nbsp;CH&nbsp;value;<br />}<br /></span><span style="color: #0000FF; ">enum</span><span style="color: #000000; ">&nbsp;ECG{Normal,Abnormal};<br /></span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;NodeECG&nbsp;</span><span style="color: #0000FF; ">extends</span><span style="color: #000000; ">&nbsp;Node{<br />&nbsp;&nbsp;&nbsp;&nbsp;ECG&nbsp;value;<br />}<br /></span><span style="color: #0000FF; ">enum</span><span style="color: #000000; ">&nbsp;HR{Low,High};<br /></span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;NodeHR&nbsp;</span><span style="color: #0000FF; ">extends</span><span style="color: #000000; ">&nbsp;Node{<br />&nbsp;&nbsp;&nbsp;&nbsp;HR&nbsp;value;<br />}<br /></span><span style="color: #0000FF; ">enum</span><span style="color: #000000; ">&nbsp;EIA{No,Yes};<br /></span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;NodeEIA&nbsp;</span><span style="color: #0000FF; ">extends</span><span style="color: #000000; ">&nbsp;Node{<br />&nbsp;&nbsp;&nbsp;&nbsp;EIA&nbsp;value;<br />}<br /></span><span style="color: #0000FF; ">enum</span><span style="color: #000000; ">&nbsp;HD{No,Yes};<br /></span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;NodeHD&nbsp;</span><span style="color: #0000FF; ">extends</span><span style="color: #000000; ">&nbsp;Node{<br />&nbsp;&nbsp;&nbsp;&nbsp;HD&nbsp;value;<br />}<br /><br /></span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;Node{<br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;Vector</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">Node</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">&nbsp;parent;<br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;name;<br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;cpt[];<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;&nbsp;Node(){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.parent</span><span style="color: #000000; ">=</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&nbsp;Vector</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">Node</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cpt</span><span style="color: #000000; ">=</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">[</span><span style="color: #000000; ">64</span><span style="color: #000000; ">];<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">static</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;main(String[]&nbsp;args)&nbsp;</span><span style="color: #0000FF; ">throws</span><span style="color: #000000; ">&nbsp;IllegalArgumentException,&nbsp;InstantiationException,&nbsp;IllegalAccessException,&nbsp;InvocationTargetException,&nbsp;Exception,&nbsp;NoSuchMethodException<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;Vector&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">Node</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">&nbsp;nodeList</span><span style="color: #000000; ">=</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&nbsp;Vector</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">Node</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">();<br />&nbsp;&nbsp;&nbsp;&nbsp;String[]&nbsp;nameList</span><span style="color: #000000; ">=</span><span style="color: #000000; ">{</span><span style="color: #000000; ">"</span><span style="color: #000000; ">A</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,</span><span style="color: #000000; ">"</span><span style="color: #000000; ">G</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,</span><span style="color: #000000; ">"</span><span style="color: #000000; ">CP</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,</span><span style="color: #000000; ">"</span><span style="color: #000000; ">BP</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,</span><span style="color: #000000; ">"</span><span style="color: #000000; ">CH</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,</span><span style="color: #000000; ">"</span><span style="color: #000000; ">ECG</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,</span><span style="color: #000000; ">"</span><span style="color: #000000; ">HR</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,</span><span style="color: #000000; ">"</span><span style="color: #000000; ">EIA</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,</span><span style="color: #000000; ">"</span><span style="color: #000000; ">HD</span><span style="color: #000000; ">"</span><span style="color: #000000; ">};<br />&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(NodeA.</span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(String&nbsp;a:nameList)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class&nbsp;newoneClass&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;Class.forName(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">homework1.BayesianNetwork$Node</span><span style="color: #000000; ">"</span><span style="color: #000000; ">+</span><span style="color: #000000; ">a);//</span><span style="color: red; ">classname也不一样</span><span style="color: #000000; "><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Constructor&nbsp;cons&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;newoneClass.getDeclaredConstructors()[</span><span style="color: #000000; ">0</span><span style="color: #000000; ">];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cons.setAccessible(</span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object&nbsp;node</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;cons.newInstance(</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&nbsp;BayesianNetwork()); //</span><span style="color: red; ">需要的是外部类的构造方法 &nbsp;&nbsp;}<br /><br />}</span><span style="color: #000000; "><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nodeList.add((Node)&nbsp;node);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span></div><img src ="http://www.cppblog.com/luyulaile/aggbug/165714.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2012-02-16 09:17 <a href="http://www.cppblog.com/luyulaile/archive/2012/02/16/165714.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java enum 类型的遍历</title><link>http://www.cppblog.com/luyulaile/archive/2011/12/27/162900.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Tue, 27 Dec 2011 04:40:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2011/12/27/162900.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/162900.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2011/12/27/162900.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/162900.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/162900.html</trackback:ping><description><![CDATA[Java enum 枚举的遍历<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #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; ">private</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">enum</span><span style="color: #000000; ">&nbsp;Action{left,right,forward,continued&nbsp;};<br /><br />&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(Action&nbsp;action:Action.values())<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">Use&nbsp;variable&nbsp;action</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">}</span></div><img src ="http://www.cppblog.com/luyulaile/aggbug/162900.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2011-12-27 12:40 <a href="http://www.cppblog.com/luyulaile/archive/2011/12/27/162900.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java Hashtable HashMap ArrayList Vector 等等</title><link>http://www.cppblog.com/luyulaile/archive/2011/12/27/162899.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Tue, 27 Dec 2011 04:00:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2011/12/27/162899.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/162899.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2011/12/27/162899.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/162899.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/162899.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: http://selives.blogbus.com/logs/27714366.htmlArrayList&nbsp;和Vector是采用数组方式存储数据，此数组元素数大于实际存储的数据以便增加和插入元素，都允许直接序号索引元素，但是插入数据要设计到数组元素移动等内存操作，所以索引数据快插入数据慢，Vector由于使用了synchronized方法（线程安全）所以性能上比ArrayList要差，...&nbsp;&nbsp;<a href='http://www.cppblog.com/luyulaile/archive/2011/12/27/162899.html'>阅读全文</a><img src ="http://www.cppblog.com/luyulaile/aggbug/162899.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2011-12-27 12:00 <a href="http://www.cppblog.com/luyulaile/archive/2011/12/27/162899.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Eclipse快捷键大全[zt]</title><link>http://www.cppblog.com/luyulaile/archive/2011/11/17/160334.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Thu, 17 Nov 2011 04:47:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2011/11/17/160334.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/160334.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2011/11/17/160334.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/160334.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/160334.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="color: #222222; font-family: arial, sans-serif; line-height: 16px; background-color: #ffffff; font-size: small; "><div><a href="http://blog.csdn.net/zt2oo8/article/details/959280">http://blog.csdn.net/zt2oo8/article/details/959280</a></div>有时我们复制到<em style="color: #d14836; font-style: normal; ">Eclipse</em>中的代码比较混乱，这时按Ctrl + Shift + F就</span><span class="Apple-style-span" style="font-family: arial, sans-serif; line-height: 16px; background-color: #ffffff; font-size: small; color: red; ">可以自动</span><span class="Apple-style-span" style="font-family: arial, sans-serif; line-height: 16px; background-color: #ffffff; font-size: small; color: red; "><em style="color: #d14836; font-style: normal; ">调整</em>好代码的<em style="color: #d14836; font-style: normal; ">格式</em></span><span class="Apple-style-span" style="font-family: arial, sans-serif; line-height: 16px; background-color: #ffffff; font-size: small; color: red; "><em style="color: #d14836; font-style: normal; "><br /><br /></em></span><span class="Apple-style-span" style="color: #4b4b4b; font-family: georgia, verdana, Arial, helvetica, sans-seriff; line-height: 20px; background-color: #ffffff; font-size: small; ">Ctrl+1&nbsp;快速修复(最经典的快捷键,就不用多说了)<br />Ctrl+D:&nbsp;删除当前行&nbsp;<br />Ctrl+Alt+&#8595;&nbsp;复制当前行到下一行(复制增加)<br />Ctrl+Alt+&#8593;&nbsp;复制当前行到上一行(复制增加)<br />Alt+&#8595;&nbsp;当前行和下面一行交互位置(特别实用,可以省去先剪切,再粘贴了)<br />Alt+&#8593;&nbsp;当前行和上面一行交互位置(同上)<br />Alt+&#8592;&nbsp;前一个编辑的页面<br />Alt+&#8594;&nbsp;下一个编辑的页面(当然是针对上面那条来说了)<br />Alt+Enter&nbsp;显示当前选择资源(工程,or&nbsp;文件&nbsp;or文件)的属性<br />Shift+Enter&nbsp;在当前行的下一行插入空行(这时鼠标可以在当前行的任一位置,不一定是最后)<br />Shift+Ctrl+Enter&nbsp;在当前行插入空行(原理同上条)<br />Ctrl+Q&nbsp;定位到最后编辑的地方<br />Ctrl+L&nbsp;定位在某行&nbsp;(对于程序超过100的人就有福音了)<br />Ctrl+M&nbsp;最大化当前的Edit或View&nbsp;(再按则反之)<br />Ctrl+/&nbsp;注释当前行,再按则取消注释<br />Ctrl+O&nbsp;快速显示&nbsp;OutLine<br />Ctrl+T&nbsp;快速显示当前类的继承结构<br />Ctrl+W&nbsp;关闭当前Editer<br />Ctrl+K&nbsp;参照选中的Word快速定位到下一个<br />Ctrl+E&nbsp;快速显示当前Editer的下拉列表(如果当前页面没有显示的用黑体表示)<br />Ctrl+/(小键盘)&nbsp;折叠当前类中的所有代码<br />Ctrl+&#215;(小键盘)&nbsp;展开当前类中的所有代码<br />Ctrl+Space&nbsp;代码助手完成一些代码的插入(但一般和输入法有冲突,可以修改输入法的热键,也可以暂用Alt+/来代替)<br />Ctrl+Shift+E&nbsp;显示管理当前打开的所有的View的管理器(可以选择关闭,激活等操作)<br />Ctrl+J&nbsp;正向增量查找(按下Ctrl+J后,你所输入的每个字母编辑器都提供快速匹配定位到某个单词,如果没有,则在stutes&nbsp;line中显示没有找到了,查一个单词时,特别实用,这个功能Idea两年前就有了)<br />Ctrl+Shift+J&nbsp;反向增量查找(和上条相同,只不过是从后往前查)<br />Ctrl+Shift+F4&nbsp;关闭所有打开的Editer<br />Ctrl+Shift+X&nbsp;把当前选中的文本全部变味小写<br />Ctrl+Shift+Y&nbsp;把当前选中的文本全部变为小写<br />Ctrl+Shift+F&nbsp;格式化当前代码<br />Ctrl+Shift+P&nbsp;定位到对于的匹配符(譬如{})&nbsp;(从前面定位后面时,光标要在匹配符里面,后面到前面,则反之)<br /><br />下面的快捷键是重构里面常用的,本人就自己喜欢且常用的整理一下(注:一般重构的快捷键都是Alt+Shift开头的了)<br />Alt+Shift+R&nbsp;重命名&nbsp;(是我自己最爱用的一个了,尤其是变量和类的Rename,比手工方法能节省很多劳动力)<br />Alt+Shift+M&nbsp;抽取方法&nbsp;(这是重构里面最常用的方法之一了,尤其是对一大堆泥团代码有用)<br />Alt+Shift+C&nbsp;修改函数结构(比较实用,有N个函数调用了这个方法,修改一次搞定)<br />Alt+Shift+L&nbsp;抽取本地变量(&nbsp;可以直接把一些魔法数字和字符串抽取成一个变量,尤其是多处调用的时候)<br />Alt+Shift+F&nbsp;把Class中的local变量变为field变量&nbsp;(比较实用的功能)<br />Alt+Shift+I&nbsp;合并变量(可能这样说有点不妥Inline)<br />Alt+Shift+V&nbsp;移动函数和变量(不怎么常用)<br />Alt+Shift+Z&nbsp;重构的后悔药(Undo)<br /><br />编辑<br />作用域&nbsp;功能&nbsp;快捷键&nbsp;<br />全局&nbsp;查找并替换&nbsp;Ctrl+F&nbsp;<br />文本编辑器&nbsp;查找上一个&nbsp;Ctrl+Shift+K&nbsp;<br />文本编辑器&nbsp;查找下一个&nbsp;Ctrl+K&nbsp;<br />全局&nbsp;撤销&nbsp;Ctrl+Z&nbsp;<br />全局&nbsp;复制&nbsp;Ctrl+C&nbsp;<br />全局&nbsp;恢复上一个选择&nbsp;Alt+Shift+&#8595;&nbsp;<br />全局&nbsp;剪切&nbsp;Ctrl+X&nbsp;<br />全局&nbsp;快速修正&nbsp;Ctrl1+1&nbsp;<br />全局&nbsp;内容辅助&nbsp;Alt+/&nbsp;<br />全局&nbsp;全部选中&nbsp;Ctrl+A&nbsp;<br />全局&nbsp;删除&nbsp;Delete&nbsp;<br />全局&nbsp;上下文信息&nbsp;Alt+？<br />Alt+Shift+?<br />Ctrl+Shift+Space&nbsp;<br />Java编辑器&nbsp;显示工具提示描述&nbsp;F2&nbsp;<br />Java编辑器&nbsp;选择封装元素&nbsp;Alt+Shift+&#8593;&nbsp;<br />Java编辑器&nbsp;选择上一个元素&nbsp;Alt+Shift+&#8592;&nbsp;<br />Java编辑器&nbsp;选择下一个元素&nbsp;Alt+Shift+&#8594;&nbsp;<br />文本编辑器&nbsp;增量查找&nbsp;Ctrl+J&nbsp;<br />文本编辑器&nbsp;增量逆向查找&nbsp;Ctrl+Shift+J&nbsp;<br />全局&nbsp;粘贴&nbsp;Ctrl+V&nbsp;<br />全局&nbsp;重做&nbsp;Ctrl+Y&nbsp;<br /><br />&nbsp;<br />查看<br />作用域&nbsp;功能&nbsp;快捷键&nbsp;<br />全局&nbsp;放大&nbsp;Ctrl+=&nbsp;<br />全局&nbsp;缩小&nbsp;Ctrl+-&nbsp;<br /><br />&nbsp;<br />窗口<br />作用域&nbsp;功能&nbsp;快捷键&nbsp;<br />全局&nbsp;激活编辑器&nbsp;F12&nbsp;<br />全局&nbsp;切换编辑器&nbsp;Ctrl+Shift+W&nbsp;<br />全局&nbsp;上一个编辑器&nbsp;Ctrl+Shift+F6&nbsp;<br />全局&nbsp;上一个视图&nbsp;Ctrl+Shift+F7&nbsp;<br />全局&nbsp;上一个透视图&nbsp;Ctrl+Shift+F8&nbsp;<br />全局&nbsp;下一个编辑器&nbsp;Ctrl+F6&nbsp;<br />全局&nbsp;下一个视图&nbsp;Ctrl+F7&nbsp;<br />全局&nbsp;下一个透视图&nbsp;Ctrl+F8&nbsp;<br />文本编辑器&nbsp;显示标尺上下文菜单&nbsp;Ctrl+W&nbsp;<br />全局&nbsp;显示视图菜单&nbsp;Ctrl+F10&nbsp;<br />全局&nbsp;显示系统菜单&nbsp;Alt+-&nbsp;<br /><br />&nbsp;<br />导航<br />作用域&nbsp;功能&nbsp;快捷键&nbsp;<br />Java编辑器&nbsp;打开结构&nbsp;Ctrl+F3&nbsp;<br />全局&nbsp;打开类型&nbsp;Ctrl+Shift+T&nbsp;<br />全局&nbsp;打开类型层次结构&nbsp;F4&nbsp;<br />全局&nbsp;打开声明&nbsp;F3&nbsp;<br />全局&nbsp;打开外部javadoc&nbsp;Shift+F2&nbsp;<br />全局&nbsp;打开资源&nbsp;Ctrl+Shift+R&nbsp;<br />全局&nbsp;后退历史记录&nbsp;Alt+&#8592;&nbsp;<br />全局&nbsp;前进历史记录&nbsp;Alt+&#8594;&nbsp;<br />全局&nbsp;上一个&nbsp;Ctrl+,&nbsp;<br />全局&nbsp;下一个&nbsp;Ctrl+.&nbsp;<br />Java编辑器&nbsp;显示大纲&nbsp;Ctrl+O&nbsp;<br />全局&nbsp;在层次结构中打开类型&nbsp;Ctrl+Shift+H&nbsp;<br />全局&nbsp;转至匹配的括号&nbsp;Ctrl+Shift+P&nbsp;<br />全局&nbsp;转至上一个编辑位置&nbsp;Ctrl+Q&nbsp;<br />Java编辑器&nbsp;转至上一个成员&nbsp;Ctrl+Shift+&#8593;&nbsp;<br />Java编辑器&nbsp;转至下一个成员&nbsp;Ctrl+Shift+&#8595;&nbsp;<br />文本编辑器&nbsp;转至行&nbsp;Ctrl+L&nbsp;<br /><br />&nbsp;<br />搜索<br />作用域&nbsp;功能&nbsp;快捷键&nbsp;<br />全局&nbsp;出现在文件中&nbsp;Ctrl+Shift+U&nbsp;<br />全局&nbsp;打开搜索对话框&nbsp;Ctrl+H&nbsp;<br />全局&nbsp;工作区中的声明&nbsp;Ctrl+G&nbsp;<br />全局&nbsp;工作区中的引用&nbsp;Ctrl+Shift+G&nbsp;<br /><br />&nbsp;<br />文本编辑<br />作用域&nbsp;功能&nbsp;快捷键&nbsp;<br />文本编辑器&nbsp;改写切换&nbsp;Insert&nbsp;<br />文本编辑器&nbsp;上滚行&nbsp;Ctrl+&#8593;&nbsp;<br />文本编辑器&nbsp;下滚行&nbsp;Ctrl+&#8595;&nbsp;<br /><br />&nbsp;<br />文件<br />作用域&nbsp;功能&nbsp;快捷键&nbsp;<br />全局&nbsp;保存&nbsp;Ctrl+X&nbsp;<br />Ctrl+S&nbsp;<br />全局&nbsp;打印&nbsp;Ctrl+P&nbsp;<br />全局&nbsp;关闭&nbsp;Ctrl+F4&nbsp;<br />全局&nbsp;全部保存&nbsp;Ctrl+Shift+S&nbsp;<br />全局&nbsp;全部关闭&nbsp;Ctrl+Shift+F4&nbsp;<br />全局&nbsp;属性&nbsp;Alt+Enter&nbsp;<br />全局&nbsp;新建&nbsp;Ctrl+N&nbsp;<br /><br />&nbsp;<br />项目<br />作用域&nbsp;功能&nbsp;快捷键&nbsp;<br />全局&nbsp;全部构建&nbsp;Ctrl+B&nbsp;<br /><br />&nbsp;<br />源代码<br />作用域&nbsp;功能&nbsp;快捷键&nbsp;<br />Java编辑器&nbsp;格式化&nbsp;Ctrl+Shift+F&nbsp;<br />Java编辑器&nbsp;取消注释&nbsp;Ctrl+\&nbsp;<br />Java编辑器&nbsp;注释&nbsp;Ctrl+/&nbsp;<br />Java编辑器&nbsp;添加导入&nbsp;Ctrl+Shift+M&nbsp;<br />Java编辑器&nbsp;组织导入&nbsp;Ctrl+Shift+O&nbsp;<br />Java编辑器&nbsp;使用try/catch块来包围&nbsp;未设置，太常用了，所以在这里列出,建议自己设置。<br />也可以使用Ctrl+1自动修正。&nbsp;<br /><br />&nbsp;<br />运行<br />作用域&nbsp;功能&nbsp;快捷键&nbsp;<br />全局&nbsp;单步返回&nbsp;F7&nbsp;<br />全局&nbsp;单步跳过&nbsp;F6&nbsp;<br />全局&nbsp;单步跳入&nbsp;F5&nbsp;<br />全局&nbsp;单步跳入选择&nbsp;Ctrl+F5&nbsp;<br />全局&nbsp;调试上次启动&nbsp;F11&nbsp;<br />全局&nbsp;继续&nbsp;F8&nbsp;<br />全局&nbsp;使用过滤器单步执行&nbsp;Shift+F5&nbsp;<br />全局&nbsp;添加/去除断点&nbsp;Ctrl+Shift+B&nbsp;<br />全局&nbsp;显示&nbsp;Ctrl+D&nbsp;<br />全局&nbsp;运行上次启动&nbsp;Ctrl+F11&nbsp;<br />全局&nbsp;运行至行&nbsp;Ctrl+R&nbsp;<br />全局&nbsp;执行&nbsp;Ctrl+U&nbsp;<br /><br />&nbsp;<br />重构<br />作用域&nbsp;功能&nbsp;快捷键&nbsp;<br />全局&nbsp;撤销重构&nbsp;Alt+Shift+Z&nbsp;<br />全局&nbsp;抽取方法&nbsp;Alt+Shift+M&nbsp;<br />全局&nbsp;抽取局部变量&nbsp;Alt+Shift+L&nbsp;<br />全局&nbsp;内联&nbsp;Alt+Shift+I&nbsp;<br />全局&nbsp;移动&nbsp;Alt+Shift+V&nbsp;<br />全局&nbsp;重命名&nbsp;Alt+Shift+R&nbsp;<br />全局&nbsp;重做&nbsp;Alt+Shift+Y</span><span class="Apple-style-span" style="color: #222222; font-family: arial, sans-serif; line-height: 16px; background-color: #ffffff; font-size: small; "><em style="color: #d14836; font-style: normal; "><br /><br /></em></span><img src ="http://www.cppblog.com/luyulaile/aggbug/160334.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2011-11-17 12:47 <a href="http://www.cppblog.com/luyulaile/archive/2011/11/17/160334.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java 中scanner 可以代替C中的Scanf[zt]</title><link>http://www.cppblog.com/luyulaile/archive/2011/11/17/160321.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Thu, 17 Nov 2011 02:57:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2011/11/17/160321.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/160321.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2011/11/17/160321.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/160321.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/160321.html</trackback:ping><description><![CDATA[<div>
<div style="clear: both; margin-top: 0px; margin-right: 20px; margin-bottom: 0px; margin-left: 20px; padding-top: 5px; ">
<h2><div style="clear: both; margin-top: 0px; margin-right: 20px; margin-bottom: 0px; margin-left: 20px; padding-top: 5px; color: #353833; font-family: Arial, Helvetica, sans-serif; font-size: 12px; "><h2><div><div><div><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; "></p><div>源文： &nbsp;http://hi.baidu.com/yfwz100/blog/item/61f8c08bf0ff79cbfd1f1098.html</div><span class="Apple-style-span" style="font-weight: normal;">从C语言转到Java的时候，对Java的输入输出可能会颇为沮丧，因为Java没有显式提供scanf()/printf()的函数。</span><p>&nbsp;</p><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; ">其实，Java给我们提供了格式化输入输出的面向对象方式。</p><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; ">格式化输出：</p><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; ">最简单的就是直接使用重载了的&#8220;+&#8221;来连接各种数据，这样比printf()个直观的方法。但也有缺点，太多的&#8220;+&#8221;号反而打乱所有的输出，而且输出格式不好控制。</p><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; ">还有一种方式是使用InputStream的format()方法，或者，如果使用的JDK版本在5.0以后的话，可以考虑相同的printf()方法。这两个方法和printf()是相同的。</p><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; ">格式化输入：</p><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; ">这个问题比较困难，我现在找到的方法都是从JDK 5.0以后引入的。而且，还是没有像scanf()那样直接替代的方法。但我们可以使用java.util.Scanner。但可惜的是</p><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; ">sc.nextInt();</p><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; ">是读取一行数据，然后转换为整数，而我们在使用scanf()时，可能会这样：</p><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; ">scanf("%d %d", &amp;a, &amp;b);</p><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; ">采用空格分割两个整数的输入，对比之下，java.util.Scanner类提供的方法就显得局限了。</p><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; ">但事实上，java.util.Scanner类提供了<span style="font-family: monospace; white-space: pre; line-height: normal; ">useDelimiter()方法来指明分割符。注意，&#8220;分割符&#8221;是使用正则表达式的形式，参看<a href="http://download.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html" target="_blank" style="text-decoration: none; color: #5fa207; ">Pattern类</a>。</span></p><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; "><span style="font-family: monospace; "><span style="line-height: normal; white-space: pre; ">如上述</span></span></p><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; "><span style="font-family: monospace; "><span style="line-height: normal; white-space: pre; ">scanf("%d %d", &amp;a, &amp;b);</span></span></p><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; "><span style="font-family: monospace; "><span style="line-height: normal; white-space: pre; ">替代为</span></span></p><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; "><span style="font-family: monospace; "><span style="line-height: normal; white-space: pre; ">sc.useDelimiter("\\s+");a=sc.nextInt();b=sc.nextInt();</span></span></p><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; "><span style="font-family: monospace; "><span style="line-height: normal; white-space: pre; ">来替代（假设sc是一个java.util.Scanner类对象）。注意，在Java正则表达式中，&#8220;\s&#8221;代表了[\t\n\x0B\f\r ]任一者（表示空白字符）。而&#8220;+&#8221;表示不仅仅一个，即连续的&#8220;空白字符&#8221;也可以。</span></span></p><span style="color: #333333; font-weight: normal; background-color: #ffffff; font-family: Helvetica, Arial, sans-serif; line-height: normal; font-size: medium; "><a name="sum" href="http:" style="text-decoration: none; color: rgb(95, 162, 7); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "></a></span><p 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; color: #333333; font-family: tahoma, arial, 宋体, sans-serif; font-size: 14px; font-weight: normal; background-color: #ffffff; "><span style="font-family: monospace; "><span style="line-height: normal; white-space: pre; ">详细信息参见<a href="http://download.oracle.com/javase/7/docs/api/java/util/Scanner.html" target="_blank" style="text-decoration: none; color: #5fa207; ">java.util.Scanner类</a>。</span></span></p></div><br /><a href="http://download.oracle.com/javase/7/docs/api/java/util/Scanner.html">http://download.oracle.com/javase/7/docs/api/java/util/Scanner.html</a></div></div></h2></div></h2></div>
</div><img src ="http://www.cppblog.com/luyulaile/aggbug/160321.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2011-11-17 10:57 <a href="http://www.cppblog.com/luyulaile/archive/2011/11/17/160321.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java IO</title><link>http://www.cppblog.com/luyulaile/archive/2011/11/17/160312.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Thu, 17 Nov 2011 01:30:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2011/11/17/160312.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/160312.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2011/11/17/160312.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/160312.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/160312.html</trackback:ping><description><![CDATA[<p><span style="font-family: simsun; line-height: 23px; ">BufferedReader &nbsp; in= &nbsp; new &nbsp; BufferedReader(new &nbsp; FileReader( "d:\\co\\work3.sql "));&nbsp;</span><br style="font-family: simsun; line-height: 23px; text-align: left; " /><span style="font-family: simsun; line-height: 23px; text-align: left; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String &nbsp; input=new &nbsp; String();&nbsp;</span><br style="font-family: simsun; line-height: 23px; text-align: left; " /><span style="font-family: simsun; line-height: 23px; text-align: left; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String &nbsp; tablename=new &nbsp; String( " ");&nbsp;</span><br style="font-family: simsun; line-height: 23px; text-align: left; " /><span style="font-family: simsun; line-height: 23px; text-align: left; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuffer &nbsp; transresult=new &nbsp; StringBuffer();&nbsp;</span><br style="font-family: simsun; line-height: 23px; text-align: left; " /><span style="font-family: simsun; line-height: 23px; text-align: left; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int &nbsp; lbracketpos,rbracketpos;&nbsp;</span><br style="font-family: simsun; line-height: 23px; text-align: left; " /><span style="font-family: simsun; line-height: 23px; text-align: left; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean &nbsp; newtable;&nbsp;</span><br style="font-family: simsun; line-height: 23px; text-align: left; " /><span style="font-family: simsun; line-height: 23px; text-align: left; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input=in.readLine();</span></p><p><span style="font-family: simsun; line-height: 23px; text-align: left; "><br /></span></p><p><span style="font-family: simsun; line-height: 23px; text-align: left; ">-------</span></p><p><span style="font-family: simsun; line-height: 23px; ">用java.io.RandomAccessFile的readLine()方法&nbsp;</span></p><p><span style="font-family: simsun; line-height: 23px; ">------</span></p><p><span style="font-family: simsun; line-height: 23px; ">FileReader对象只有read方法<br />---------------<br /></span><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">/*&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">* 简单的读/写文本文件的示例&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">* 这里包含了三个例子，即&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">* 1.&nbsp;</span><span class="goog_qs-tidbit goog_qs-tidbit-0" style="padding: 0px; margin: 0px; background-color: #ffffdd; text-decoration: inherit; display: inline !important; line-height: 25px; font-family: Arial, Tahoma, Verdana, sans-serif; ">将文件读入到内存（这里是StringBuffer）的例子</span><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">* 2. 将内容中的文本写到文件&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">* 3. 将一个文件的内容读出来写入另一个文件中&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">* 同时也展示了如果从输入流中读出来内容写入输出流中（仅限文本流）&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">* 三个例子可以独立存在，所以根据需要只看其中一个就行了。&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">*/&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span class="goog_qs-tidbit goog_qs-tidbit-1" style="padding: 0px; margin: 0px; background-color: #ffffdd; text-decoration: inherit; display: inline !important; line-height: 25px; font-family: Arial, Tahoma, Verdana, sans-serif; ">import java.io.</span><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">BufferedReader;&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">import java.io.FileInputStream;&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">import java.io.FileOutputStream;&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">import java.io.IOException;&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">import java.io.InputStream;&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">import java.io.InputStreamReader;&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">import java.io.OutputStream;&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">import java.io.OutputStreamWriter;&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">import java.io.PrintStream;&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">import java.io.PrintWriter;&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">public final class AccessTextFile {&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">/**&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">* 1. 演示将流中的文本读入一个 StringBuffer 中&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">* @throws IOException&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">*/&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">public void readToBuffer(StringBuffer buffer, InputStream is)&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">throws IOException {&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">String line; // 用来保存每行读取的内容&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">BufferedReader reader = new BufferedReader(new InputStreamReader(is));&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">line = reader.readLine(); // 读取第一行&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">while (line != null) { // 如果 line 为空说明读完了&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">buffer.append(line); // 将读到的内容添加到 buffer 中&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">buffer.append("\n"); // 添加换行符&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">line = reader.readLine(); // 读取下一行&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">}&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">}&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">/**&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">* 2. 演示将 StringBuffer 中的内容读出到流中&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">*/&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">public void writeFromBuffer(StringBuffer buffer, OutputStream os) {&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">// 用 PrintStream 可以方便的把内容输出到输出流中&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">// 其对象的用法和 System.out 一样&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">// （System.out 本身就是 PrintStream 对象）&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">PrintStream ps = new PrintStream(os);&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">ps.print(buffer.toString());&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">}&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">/**&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">* 3*. 从输入流中拷贝内容到输入流中&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">* @throws IOException&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">*/&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">public void copyStream(InputStream is, OutputStream os) throws IOException {&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">// 这个读过过程可以参阅 readToBuffer 中的注释&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">String line;&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">BufferedReader reader = new BufferedReader(new InputStreamReader(is));&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">PrintWriter writer = new PrintWriter(new OutputStreamWriter(os));&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">line = reader.readLine();&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">while (line != null) {&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">writer.println(line);&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">line = reader.readLine();&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">}&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">writer.flush(); // 最后确定要把输出流中的东西都写出去了&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">// 这里不关闭 writer 是因为 os 是从外面传进来的&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">// 既然不是从这里打开的，也就不从这里关闭&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">// 如果关闭的 writer，封装在里面的 os 也就被关了&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">}&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">/**&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">* 3. 调用 copyStream(InputStream, OutputStream) 方法拷贝文本文件&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">*/&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">public void copyTextFile(String inFilename, String outFilename)&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">throws IOException {&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">// 先根据输入/输出文件生成相应的输入/输出流&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">InputStream is = new FileInputStream(inFilename);&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">OutputStream os = new FileOutputStream(outFilename);&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">copyStream(is, os); // 用 copyStream 拷贝内容&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">is.close(); // is 是在这里打开的，所以需要关闭&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">os.close(); // os 是在这里打开的，所以需要关闭&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">}&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">public static void main(String[] args) throws IOException {&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">int sw = 1; // 三种测试的选择开关&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">AccessTextFile test = new AccessTextFile();&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">switch (sw) {&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">case 1: // 测试读&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">{&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">InputStream is = new FileInputStream("E:\\test.txt");&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">StringBuffer buffer = new StringBuffer();&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">test.readToBuffer(buffer, is);&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">System.out.println(buffer); // 将读到 buffer 中的内容写出来&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">is.close();&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">break;&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">}&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">case 2: // 测试写&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">{&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">StringBuffer buffer = new StringBuffer("Only a test\n");&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">test.writeFromBuffer(buffer, System.out);&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">break;&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">}&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">case 3: // 测试拷贝&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">{&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">test.copyTextFile("E:\\test.txt", "E:\\r.txt");&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">}&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">break;&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">}&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">}&nbsp;</span><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><br style="padding: 0px; margin: 0px; color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; " /><span style="color: #333333; font-family: Arial, Tahoma, Verdana, sans-serif; line-height: 25px; background-color: #f7fcff; ">}&nbsp;<br /><br />==============<br /></span><p style="line-height: 19px; margin-top: 10px; margin-bottom: 10px; color: #4b4b4b; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; background-color: #ffffff; ">//这个是用reader的，这个不会出现异常</p><p style="line-height: 19px; margin-top: 10px; margin-bottom: 10px; color: #4b4b4b; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; background-color: #ffffff; ">import java.io.*;<br />import java.util.*;</p><p style="line-height: 19px; margin-top: 10px; margin-bottom: 10px; color: #4b4b4b; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; background-color: #ffffff; ">public class Test {<br />&nbsp;&nbsp;&nbsp; public static void main(String[] args) throws Exception {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; File file = new File("Test.java");<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BufferedReader reader = new BufferedReader(new FileReader(file));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String line = null;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ((line = reader.readLine()) != null) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(line);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reader.close();<br />&nbsp;&nbsp;&nbsp; }<br />}</p><p style="line-height: 19px; margin-top: 10px; margin-bottom: 10px; color: #4b4b4b; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; background-color: #ffffff; ">&nbsp;</p></p><img src ="http://www.cppblog.com/luyulaile/aggbug/160312.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2011-11-17 09:30 <a href="http://www.cppblog.com/luyulaile/archive/2011/11/17/160312.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java 计时 Java timing</title><link>http://www.cppblog.com/luyulaile/archive/2011/09/26/156894.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Mon, 26 Sep 2011 15:15:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2011/09/26/156894.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/156894.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2011/09/26/156894.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/156894.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/156894.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="color: #008000; font-family: arial; line-height: 18px; background-color: #ffffff; font-size: small; ">apps.hi.baidu.com/share/detail/33559179</span><span class="Apple-style-span" style="font-family: 'Times New Roman'; line-height: normal; font-size: medium; "><div id="appShareTitle"><br />Java 程序计时（Java Timing）</div><div id="appShareOpt"></div><div id="app-share-content"><div>（1）如何精确给代码计时；</div><div><p>（2）计时机制本身的时间开销；</p><div>经典的程序计时利用Java API方法：<font face="新宋体">System.currentTimeMillis()</font>，在待测代码前后放置一个秒表（stopwatch），计算前后时间差即可。</div><div></div><div>下面是一个引自&#8220;&#8221;的stopwatch类，</div><div></div><div><div><font size="2">import java.io.PrintStream;</font></div><div><font size="2"><br />final class Timer{&nbsp;</font><font size="2"><br />&nbsp;&nbsp;&nbsp; private long startTime;</font></div><div><font size="2">&nbsp;&nbsp;&nbsp; private long endTime;</font></div><div></div><div><font size="2">&nbsp;&nbsp;&nbsp; public Timer() {&nbsp;&nbsp; reset(); }</font></div><div><font size="2"><br />&nbsp;&nbsp;&nbsp; public void start() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.gc();</font></div><div><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; startTime = System.currentTimeMillis();</font></div><div><font size="2">&nbsp;&nbsp;&nbsp; }&nbsp;<br />&nbsp;&nbsp;&nbsp; public void end() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.gc();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; endTime = System.currentTimeMillis();</font></div><div><font size="2">&nbsp;&nbsp;&nbsp;&nbsp; }</font></div><div></div><div><font size="2">&nbsp;&nbsp;&nbsp; public long duration() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (endTime - startTime);</font></div><div><font size="2">&nbsp;&nbsp;&nbsp; }</font></div><div><font size="2">&nbsp;&nbsp;&nbsp; public void printDuration( PrintStream out ) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long elapsedTimeInSecond = duration() / 1000;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long remainderInMillis = duration() % 1000;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; out.println("\nTotal execution time:"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + elapsedTimeInSecond + "."<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + remainderInMillis<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + " seconds");</font></div><div><font size="2">&nbsp;&nbsp;&nbsp; }</font></div><div><font size="2">&nbsp;&nbsp;&nbsp; public void reset() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; startTime = 0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; endTime = 0;</font></div><div><font size="2">&nbsp;&nbsp;&nbsp; }</font></div><div><font size="2">&nbsp;&nbsp;&nbsp; public static void main( String[] args ) {<br /><font style="background-color: #80ffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Timer timer = new Timer();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timer.start();</font></font></div><div><font size="2" style="background-color: #80ffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 500; i++) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.print("#");<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font></div><div><font size="2" style="background-color: #80ffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timer.end();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timer.printDuration(System.out); }</font></div><div><font size="2">}</font></div></div><div></div><div>这是一种常规做法。</div><div></div><div>在多线程环境中，线程切换由OS控制，因此两次掐表之间可能记录了被OS切换到的其它线程的执行时间，这期间甚至包含其它线程的长耗时I/O操作。</div><div></div><div>多次掐表然后求其平均可一定程度上平滑这种误差，然而，对于高精度要求，上述API秒表计时方式是有缺陷的。</div><div></div><div><a target="_blank" href="http://www.javaworld.com/javaworld/feedback/jw-feedback-form.html"><strong><font color="#000033" size="2">Jesper</font></strong></a>&nbsp;在&#8220;Use the JVM Profiler Interface for accurate timing&#8221;中描述了一种利用Java Virtual Machine Profiler Interface (JVMPI)的方法<br /></div></div></div></span><span class="Apple-style-span" style="font-family: 'Segoe UI', Calibri, 'Myriad Pro', Myriad, 'Trebuchet MS', Helvetica, Arial, sans-serif; font-size: 13px; line-height: normal; background-color: #ffffff; color: red; ">发现了两个新的函数:System.currentTimeMillis()和System.nanoTime()。其中System.nanoTime()是1.5版以后的新函数。这两个函数都是返回当前系统的时间，只是System.nanoTime()返回的精度更高。</span><span class="Apple-style-span" style="font-family: 'Times New Roman'; line-height: normal; font-size: medium; "><div><div><div></div></div></div></span><img src ="http://www.cppblog.com/luyulaile/aggbug/156894.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2011-09-26 23:15 <a href="http://www.cppblog.com/luyulaile/archive/2011/09/26/156894.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java多态中的方法覆盖</title><link>http://www.cppblog.com/luyulaile/archive/2011/04/08/143742.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Fri, 08 Apr 2011 10:35:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2011/04/08/143742.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/143742.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2011/04/08/143742.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/143742.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/143742.html</trackback:ping><description><![CDATA[<span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium 'Times New Roman'; white-space: normal; orphans: 2; letter-spacing: normal; color: #000000; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"><span style="line-height: 20px; font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px" class="Apple-style-span">多态&nbsp;(Polymorphism)&nbsp;大家应该都不陌生，它是我们开发面向对象系统的&#8220;老朋友&#8221;了&nbsp;。但是老朋友也会有&#8220;烦心&#8221;的时候啊，呵呵。有时候不注意，还真会被它难到。譬如下面这个例子（thank&nbsp;Hayden）。大家可以先不看下面的答案，在自己脑海中运行一道，看看自己想的跟实际结果是否相符。<br style="line-height: normal" />
<div style="border-bottom: #cccccc 1px solid; filter: none; border-left: #cccccc 1px solid; padding-bottom: 5px; line-height: normal; background-color: #f3f3f3; margin: 5px 20px; padding-left: 5px; padding-right: 5px; zoom: 1 !important; font-family: Arial; word-wrap: break-word; visibility: visible !important; font-size: 14px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 5px; background-origin: initial; background-clip: initial">public&nbsp;class&nbsp;Polymorphism{<br style="line-height: normal" />&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;{<br style="line-height: normal" />&nbsp;&nbsp;A&nbsp;b&nbsp;=&nbsp;new&nbsp;B();<br style="line-height: normal" />&nbsp;&nbsp;b.fb();<br style="line-height: normal" />&nbsp;}<br style="line-height: normal" />}</div>
<div style="border-bottom: #cccccc 1px solid; filter: none; border-left: #cccccc 1px solid; padding-bottom: 5px; line-height: normal; background-color: #f3f3f3; margin: 5px 20px; padding-left: 5px; padding-right: 5px; zoom: 1 !important; font-family: Arial; word-wrap: break-word; visibility: visible !important; font-size: 14px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 5px; background-origin: initial; background-clip: initial">class&nbsp;A&nbsp;{<br style="line-height: normal" />&nbsp;public&nbsp;A(){<br style="line-height: normal" />&nbsp;&nbsp;<br style="line-height: normal" />&nbsp;}<br style="line-height: normal" />&nbsp;public&nbsp;void&nbsp;fa()&nbsp;{<br style="line-height: normal" />&nbsp;&nbsp;System.out.println("CLASS&nbsp;A&nbsp;:Function&nbsp;fa&nbsp;Runing......");<br style="line-height: normal" />&nbsp;}<br style="line-height: normal" /><br style="line-height: normal" />&nbsp;public&nbsp;void&nbsp;fb()&nbsp;{<br style="line-height: normal" />&nbsp;&nbsp;System.out.println("CLASS&nbsp;A&nbsp;:Function&nbsp;fb&nbsp;Runing......");<br style="line-height: normal" />&nbsp;&nbsp;fa();<br style="line-height: normal" />&nbsp;&nbsp;System.out.println("CLASS&nbsp;A&nbsp;:Function&nbsp;fb&nbsp;Stop......");<br style="line-height: normal" />&nbsp;}<br style="line-height: normal" />}</div>
<div style="border-bottom: #cccccc 1px solid; filter: none; border-left: #cccccc 1px solid; padding-bottom: 5px; line-height: normal; background-color: #f3f3f3; margin: 5px 20px; padding-left: 5px; padding-right: 5px; zoom: 1 !important; font-family: Arial; word-wrap: break-word; visibility: visible !important; font-size: 14px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 5px; background-origin: initial; background-clip: initial">class&nbsp;B&nbsp;extends&nbsp;A&nbsp;{<br style="line-height: normal" />&nbsp;public&nbsp;B(){<br style="line-height: normal" />&nbsp;}<br style="line-height: normal" />&nbsp;public&nbsp;void&nbsp;fa()&nbsp;{<br style="line-height: normal" />&nbsp;&nbsp;System.out.println("CLASS&nbsp;B&nbsp;:Function&nbsp;fa&nbsp;Runing......");<br style="line-height: normal" />&nbsp;}<br style="line-height: normal" /><br style="line-height: normal" />&nbsp;public&nbsp;void&nbsp;fb()&nbsp;{<br style="line-height: normal" />&nbsp;&nbsp;System.out.println("CLASS&nbsp;B&nbsp;:Function&nbsp;fb&nbsp;Runing......");<br style="line-height: normal" />&nbsp;&nbsp;super.fb();<br style="line-height: normal" />&nbsp;&nbsp;System.out.println("CLASS&nbsp;B&nbsp;:Function&nbsp;fb&nbsp;Stop......");<br style="line-height: normal" />&nbsp;}<br style="line-height: normal" />}</div>
下面是它的运行结果：<br style="line-height: normal" />
<div style="border-bottom: #cccccc 1px solid; filter: none; border-left: #cccccc 1px solid; padding-bottom: 5px; line-height: normal; background-color: #f3f3f3; margin: 5px 20px; padding-left: 5px; padding-right: 5px; zoom: 1 !important; font-family: Arial; word-wrap: break-word; visibility: visible !important; font-size: 14px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 5px; background-origin: initial; background-clip: initial">CLASS&nbsp;B&nbsp;:Function&nbsp;fb&nbsp;Runing......<br style="line-height: normal" />CLASS&nbsp;A&nbsp;:Function&nbsp;fb&nbsp;Runing......<br style="line-height: normal" />CLASS&nbsp;B&nbsp;:Function&nbsp;fa&nbsp;Runing......<br style="line-height: normal" />CLASS&nbsp;A&nbsp;:Function&nbsp;fb&nbsp;Stop......<br style="line-height: normal" />CLASS&nbsp;B&nbsp;:Function&nbsp;fb&nbsp;Stop......</div>
<p style="line-height: normal">&nbsp;&nbsp;&nbsp;怎么样，猜对结果了吗？如果结果跟你想象的一模一样，那么恭喜你，你对多态已经有初步了解了，起码在语法层次上是比较熟悉了。但是，千万不要&#8220;洋洋得意&#8221;，你可否解析结果为什么会是这样吗？我们可以先来梳理一下程序流程：<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;1、运行main函数，创建B对象(<span style="color: red">A b = new B(),完全可以替代B b=new B();</span>)，调用B的方法fb，于是打印出"CLASS&nbsp;B&nbsp;:Function&nbsp;fb&nbsp;Runing......"，都在情理之中。<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;2、执行super.fb()，调用父类A的方法fb，首先打印出"CLASS&nbsp;A&nbsp;:Function&nbsp;fb&nbsp;Runing......"，预料之中<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;3、执行方法fa()，打印出"CLASS&nbsp;B&nbsp;:Function&nbsp;fa&nbsp;Runing......"，呃？奇怪了，为什么不是执行A的方法fa(),而是子类B中的fa()呢？<strong style="line-height: normal">当前被执行的是类A的方法，那么虚拟机找到的应该是A类的Method&nbsp;Table，找到的应该是A类的方法fa()啊？</strong>难解～<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;4、打印"CLASS&nbsp;A&nbsp;:Function&nbsp;fb&nbsp;Stop......"，返回<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;5、打印<strong style="line-height: normal">"</strong>CLASS&nbsp;A&nbsp;:Function&nbsp;fb&nbsp;Stop....."，返回，程序结束。<br style="line-height: normal" />&nbsp;&nbsp;现在问题清楚了，就是虚拟机在执行类A方法的时候查找的Method&nbsp;Table竟然是子类B的。为什么呢？其实，只要我们清楚java方法调用的方式，这个问题就迎刃而解了。在Java虚拟机中，每启动一个新的线程，虚拟机都会为它分配一个Java栈，而每当线程调用一个java方法时，虚拟机就会在该线程的java栈中压入一个新帧，用以存储参数，局部变量等数据。我们将这个正在被执行的方法称为该线程的当前方法，其相应的栈帧为当前帧。<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp;好了，当我们调用一个方法时，我们需要往当前帧中压入哪些参数呢？简单，方法的参数列表中不是都说得清清楚楚的吗？嗯，对于C语言来说，这个说法是正确的，但是对于诸如C++，Java，Python等面向对象语言来说，却是不对的。<span style="background-color: yellow">大家还记得那个"this"指针吗？！</span>不错，<strong style="line-height: normal">在Java中，所有的实例方法（Instance&nbsp;Method）调用的时候都会把当前对象压入当前帧中，Java虚拟机正是通过这个参数来决定当前所使用的类（通过判断该对象的类型）</strong>。<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp;在上面的例子中，main中调用b.fb()时，压入的当前对象自然是B类对象，我们记为b。在B的fb()中调用super.fb()时，压入的就是刚刚压入的对象，也就是b了。同样，在A的fb中调用fa()时，压入的也是b。因此，在使用&nbsp;invokevirtual指令调用fa()时，找的就是B的方法表（当前对象b的类型为B），也就执行了类B的fa了。<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp;这种现象在构造函数中特别常见，因为构造函数中会隐含使用调用父类的构造函数的，如果在父类的构造函数中调用了实例方法（如&nbsp;A的fa），而在子类中又覆盖了这个实例方法（如&nbsp;B的fa），那么得到的结果往往不是我们所要的。因此，我们最好不要在构造函数中使用多态方法，不然，Debug会很痛苦的：）</p>
</span></span><img src ="http://www.cppblog.com/luyulaile/aggbug/143742.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2011-04-08 18:35 <a href="http://www.cppblog.com/luyulaile/archive/2011/04/08/143742.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java 构造函数</title><link>http://www.cppblog.com/luyulaile/archive/2010/12/22/137218.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Wed, 22 Dec 2010 12:03:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2010/12/22/137218.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/137218.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2010/12/22/137218.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/137218.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/137218.html</trackback:ping><description><![CDATA[<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><img id="Codehighlighter1_17_382_Open_Image" onclick="this.style.display='none'; Codehighlighter1_17_382_Open_Text.style.display='none'; Codehighlighter1_17_382_Closed_Image.style.display='inline'; Codehighlighter1_17_382_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_17_382_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_17_382_Closed_Text.style.display='none'; Codehighlighter1_17_382_Open_Image.style.display='inline'; Codehighlighter1_17_382_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;Msg&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_17_382_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_17_382_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Agent.agent_actor&nbsp;actor;<br /><img id="Codehighlighter1_46_132_Open_Image" onclick="this.style.display='none'; Codehighlighter1_46_132_Open_Text.style.display='none'; Codehighlighter1_46_132_Closed_Image.style.display='inline'; Codehighlighter1_46_132_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_46_132_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_46_132_Closed_Text.style.display='none'; Codehighlighter1_46_132_Open_Image.style.display='inline'; Codehighlighter1_46_132_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_46_132_Closed_Text">/**&nbsp;*/</span><span id="Codehighlighter1_46_132_Open_Text"><span style="color: #008000">/**</span><span style="color: #008000">消息类型：接受对方出价;谈判失败退出;出价;对方接受自己出价谈判成功;新进入谈判;向所有对手出完价或已经退出谈判结束回答，注意ACCPET只向发出一个agent发出</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /><img id="Codehighlighter1_146_186_Open_Image" onclick="this.style.display='none'; Codehighlighter1_146_186_Open_Text.style.display='none'; Codehighlighter1_146_186_Closed_Image.style.display='inline'; Codehighlighter1_146_186_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_146_186_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_146_186_Closed_Text.style.display='none'; Codehighlighter1_146_186_Open_Image.style.display='inline'; Codehighlighter1_146_186_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">enum</span><span style="color: #000000">&nbsp;answer</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_146_186_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_146_186_Open_Text"><span style="color: #000000">{ACCEPT,REJECT,OFFER,OK,NEWMEM,ENDANSWER}</span></span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;answer&nbsp;msg_type;<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Offer&nbsp;offer;<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;from_no;<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;to_no;<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;min[];<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;max[];<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Msg(<br /><img id="Codehighlighter1_289_380_Open_Image" onclick="this.style.display='none'; Codehighlighter1_289_380_Open_Text.style.display='none'; Codehighlighter1_289_380_Closed_Image.style.display='inline'; Codehighlighter1_289_380_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_289_380_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_289_380_Closed_Text.style.display='none'; Codehighlighter1_289_380_Open_Image.style.display='inline'; Codehighlighter1_289_380_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_289_380_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_289_380_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;min</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">double</span><span style="color: #000000">[Negotiation.NUM_ISSUE</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">];&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">double</span><span style="color: #000000">[Negotiation.NUM_ISSUE</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span></div>
<br />如果对象成员中有非基本类型成员，比如上例中的 offer对象是Msg的成员，<br />则必须在构造函数中初始化该对象，否则为null。<br /><br />应该将构造函数Msg改为<br />&nbsp;Msg()<br />&nbsp;{<br />&nbsp;&nbsp;min= new double[Negotiation.NUM_ISSUE+1];&nbsp;<br />&nbsp;&nbsp;max=new double[Negotiation.NUM_ISSUE+1];<br />&nbsp;&nbsp;&nbsp;&nbsp; offer=new Offer();<br />&nbsp;}<img src ="http://www.cppblog.com/luyulaile/aggbug/137218.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2010-12-22 20:03 <a href="http://www.cppblog.com/luyulaile/archive/2010/12/22/137218.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java对象及其引用</title><link>http://www.cppblog.com/luyulaile/archive/2010/12/22/137162.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Wed, 22 Dec 2010 02:33:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2010/12/22/137162.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/137162.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2010/12/22/137162.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/137162.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/137162.html</trackback:ping><description><![CDATA[<p>&nbsp;原文地址：<span class=f><cite><font color=#0e774a>blog.<strong>csdn</strong>.net/beepbug/archive/2006/08/14/1062790.aspx</font></cite></span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 初学Java时，在很长一段时间里，总觉得基本概念很模糊。后来才知道，在许多Java书中，把对象和对象的引用混为一谈。可是，如果我分不清对象与对象引用，那实在没法很好地理解下面的面向对象技术。把自己的一点认识写下来，或许能让初学Java的朋友们少走一点弯路。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 为便于说明，我们先定义一个简单的类：<br>&nbsp;class Vehicle {<br>int passengers;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>int fuelcap;<br>int mpg;<br>}<br>有了这个模板，就可以用它来创建对象：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Vehicle veh1 = new Vehicle();<br>通常把这条语句的动作称之为创建一个对象，其实，它包含了四个动作。<br>1）右边的&#8220;new Vehicle&#8221;，是以Vehicle类为模板，在堆空间里创建一个Vehicle类对象（也简称为Vehicle对象）。<br>2）末尾的()意味着，在对象创建后，立即调用Vehicle类的构造函数，对刚生成的对象进行初始化。构造函数是肯定有的。如果你没写，Java会给你补上一个默认的构造函数。<br>3）左边的&#8220;Vehicle veh1&#8221;创建了一个Vehicle类引用变量。所谓Vehicle类引用，就是以后可以用来指向Vehicle对象的对象引用。<br>4）&#8220;=&#8221;操作符使对象引用指向刚创建的那个Vehicle对象。<br>我们可以把这条语句拆成两部分：<br>Vehicle veh1;<br>veh1 = new Vehicle();<br>效果是一样的。这样写，就比较清楚了，有两个实体：一是对象引用变量，一是对象本身。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在堆空间里创建的实体，与在数据段以及栈空间里创建的实体不同。尽管它们也是确确实实存在的实体，但是，我们看不见，也摸不着。不仅如此，我们仔细研究一下第二句，找找刚创建的对象叫什么名字？有人说，它叫&#8220;Vehicle&#8221;。不对，&#8220;Vehicle&#8221;是类（对象的创建模板）的名字。一个Vehicle类可以据此创建出无数个对象，这些对象不可能全叫&#8220;Vehicle&#8221;。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 对象连名都没有，没法直接访问它。我们只能通过对象引用来间接访问对象。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 为了形象地说明对象、引用及它们之间的关系，可以做一个或许不很妥当的比喻。对象好比是一只很大的气球，大到我们抓不住它。引用变量是一根绳，可以用来系汽球。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果只执行了第一条语句，还没执行第二条，此时创建的引用变量veh1还没指向任何一个对象，它的值是null。引用变量可以指向某个对象，或者为null。它是一根绳，一根还没有系上任何一个汽球的绳。执行了第二句后，一只新汽球做出来了，并被系在veh1这根绳上。我们抓住这根绳，就等于抓住了那只汽球。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 再来一句：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Vehicle veh2;<br>就又做了一根绳，还没系上汽球。如果再加一句：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; veh2 = veh1;<br>系上了。这里，发生了复制行为。但是，要说明的是，对象本身并没有被复制，被复制的只是对象引用。结果是，veh2也指向了veh1所指向的对象。两根绳系的是同一只汽球。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果用下句再创建一个对象：<br>veh2 = new Vehicle();<br>则引用变量veh2改指向第二个对象。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 从以上叙述再推演下去，我们可以获得以下结论：（1）一个对象引用可以指向0个或1个对象（一根绳子可以不系汽球，也可以系一个汽球）；（2）一个对象可以有N个引用指向它（可以有N条绳子系住一个汽球）。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果再来下面语句：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; veh1 = veh2;<br>按上面的推断，veh1也指向了第二个对象。这个没问题。问题是第一个对象呢？没有一条绳子系住它，它飞了。多数书里说，它被Java的垃圾回收机制回收了。这不确切。正确地说，它已成为垃圾回收机制的处理对象。至于什么时候真正被回收，那要看垃圾回收机制的心情了。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 由此看来，下面的语句应该不合法吧？至少是没用的吧？<br>new Vehicle();<br>不对。它是合法的，而且可用的。譬如，如果我们仅仅为了打印而生成一个对象，就不需要用引用变量来系住它。最常见的就是打印字符串：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&#8220;I am Java!&#8221;);<br>字符串对象&#8220;I am Java!&#8221;在打印后即被丢弃。有人把这种对象称之为临时对象。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 对象与引用的关系将持续到对象回收。但是，关于这一点，打算在下文&#8220;简述Java回收机制&#8221;再说。<br><br><br>-------------------<br>补充：<br>test类</p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">package&nbsp;test;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img id=Codehighlighter1_33_273_Open_Image onclick="this.style.display='none'; Codehighlighter1_33_273_Open_Text.style.display='none'; Codehighlighter1_33_273_Closed_Image.style.display='inline'; Codehighlighter1_33_273_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_33_273_Closed_Image onclick="this.style.display='none'; Codehighlighter1_33_273_Closed_Text.style.display='none'; Codehighlighter1_33_273_Open_Image.style.display='inline'; Codehighlighter1_33_273_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;test&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_33_273_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_33_273_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;value;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;price;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test()<br><img id=Codehighlighter1_89_136_Open_Image onclick="this.style.display='none'; Codehighlighter1_89_136_Open_Text.style.display='none'; Codehighlighter1_89_136_Closed_Image.style.display='inline'; Codehighlighter1_89_136_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_89_136_Closed_Image onclick="this.style.display='none'; Codehighlighter1_89_136_Closed_Text.style.display='none'; Codehighlighter1_89_136_Open_Image.style.display='inline'; Codehighlighter1_89_136_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_89_136_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_89_136_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.value</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.price</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img id=Codehighlighter1_177_270_Open_Image onclick="this.style.display='none'; Codehighlighter1_177_270_Open_Text.style.display='none'; Codehighlighter1_177_270_Closed_Image.style.display='inline'; Codehighlighter1_177_270_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_177_270_Closed_Image onclick="this.style.display='none'; Codehighlighter1_177_270_Closed_Text.style.display='none'; Codehighlighter1_177_270_Open_Image.style.display='inline'; Codehighlighter1_177_270_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_177_270_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_177_270_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;test&nbsp;_test</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;test();<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;fun.changeTest(_test);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;System.</span><span style="COLOR: #0000ff">out</span><span style="COLOR: #000000">.println(_test.value);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
fun：类<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">package&nbsp;test;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img id=Codehighlighter1_32_100_Open_Image onclick="this.style.display='none'; Codehighlighter1_32_100_Open_Text.style.display='none'; Codehighlighter1_32_100_Closed_Image.style.display='inline'; Codehighlighter1_32_100_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_32_100_Closed_Image onclick="this.style.display='none'; Codehighlighter1_32_100_Closed_Text.style.display='none'; Codehighlighter1_32_100_Open_Image.style.display='inline'; Codehighlighter1_32_100_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;fun&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_32_100_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_32_100_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;changeTest(test&nbsp;_test)<br><img id=Codehighlighter1_76_98_Open_Image onclick="this.style.display='none'; Codehighlighter1_76_98_Open_Text.style.display='none'; Codehighlighter1_76_98_Closed_Image.style.display='inline'; Codehighlighter1_76_98_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_76_98_Closed_Image onclick="this.style.display='none'; Codehighlighter1_76_98_Closed_Text.style.display='none'; Codehighlighter1_76_98_Open_Image.style.display='inline'; Codehighlighter1_76_98_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_76_98_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_76_98_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;_test.value</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
<br>执行号的输出为：<br>-1,<br>说明传递引用修改了对象的值。 
<img src ="http://www.cppblog.com/luyulaile/aggbug/137162.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2010-12-22 10:33 <a href="http://www.cppblog.com/luyulaile/archive/2010/12/22/137162.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java中全局变量的实现</title><link>http://www.cppblog.com/luyulaile/archive/2010/12/21/137139.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Tue, 21 Dec 2010 12:03:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2010/12/21/137139.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/137139.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2010/12/21/137139.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/137139.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/137139.html</trackback:ping><description><![CDATA[<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">import&nbsp;java.util.</span><span style="color: #000000">*</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"  alt="" />import&nbsp;java.math.</span><span style="color: #000000">*</span><span style="color: #000000">;<br /><img id="Codehighlighter1_65_304_Open_Image" onclick="this.style.display='none'; Codehighlighter1_65_304_Open_Text.style.display='none'; Codehighlighter1_65_304_Closed_Image.style.display='inline'; Codehighlighter1_65_304_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_65_304_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_65_304_Closed_Text.style.display='none'; Codehighlighter1_65_304_Open_Image.style.display='inline'; Codehighlighter1_65_304_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;Negotiation&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_65_304_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_65_304_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;final&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;&nbsp;NUM_MAXTIME</span><span style="color: #000000">=</span><span style="color: #000000">10</span><span style="color: #000000">;<br /><img id="Codehighlighter1_109_117_Open_Image" onclick="this.style.display='none'; Codehighlighter1_109_117_Open_Text.style.display='none'; Codehighlighter1_109_117_Closed_Image.style.display='inline'; Codehighlighter1_109_117_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_109_117_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_109_117_Closed_Text.style.display='none'; Codehighlighter1_109_117_Open_Image.style.display='inline'; Codehighlighter1_109_117_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_109_117_Closed_Text">/**/</span><span id="Codehighlighter1_109_117_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">*最大轮次</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;final&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;MAX_TURN</span><span style="color: #000000">=</span><span style="color: #000000">50</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;final&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;NUM_ISSUE</span><span style="color: #000000">=</span><span style="color: #000000">3</span><span style="color: #000000">;<br /><img id="Codehighlighter1_194_203_Open_Image" onclick="this.style.display='none'; Codehighlighter1_194_203_Open_Text.style.display='none'; Codehighlighter1_194_203_Closed_Image.style.display='inline'; Codehighlighter1_194_203_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_194_203_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_194_203_Closed_Text.style.display='none'; Codehighlighter1_194_203_Open_Image.style.display='inline'; Codehighlighter1_194_203_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_194_203_Closed_Text">/**/</span><span id="Codehighlighter1_194_203_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">*最大对手数</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;final&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;MAX_PARTNER</span><span style="color: #000000">=</span><span style="color: #000000">10</span><span style="color: #000000">;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /><img id="Codehighlighter1_250_266_Open_Image" onclick="this.style.display='none'; Codehighlighter1_250_266_Open_Text.style.display='none'; Codehighlighter1_250_266_Closed_Image.style.display='inline'; Codehighlighter1_250_266_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_250_266_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_250_266_Closed_Text.style.display='none'; Codehighlighter1_250_266_Open_Image.style.display='inline'; Codehighlighter1_250_266_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_250_266_Closed_Text">/**/</span><span id="Codehighlighter1_250_266_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">*谈判过程<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;negotiationProcess()<br /><img id="Codehighlighter1_295_301_Open_Image" onclick="this.style.display='none'; Codehighlighter1_295_301_Open_Text.style.display='none'; Codehighlighter1_295_301_Closed_Image.style.display='inline'; Codehighlighter1_295_301_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_295_301_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_295_301_Closed_Text.style.display='none'; Codehighlighter1_295_301_Open_Image.style.display='inline'; Codehighlighter1_295_301_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_295_301_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_295_301_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span></div>
然后在其他类中只需要调用 Negotiation.NUM_MAXTIME,NUM_ISSUE,等值<img src ="http://www.cppblog.com/luyulaile/aggbug/137139.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2010-12-21 20:03 <a href="http://www.cppblog.com/luyulaile/archive/2010/12/21/137139.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java对象数组</title><link>http://www.cppblog.com/luyulaile/archive/2010/12/21/137138.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Tue, 21 Dec 2010 11:57:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2010/12/21/137138.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/137138.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2010/12/21/137138.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/137138.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/137138.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: class Issue(){}&nbsp;class Offer&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; Issue _issue[];&nbsp; Offer()&nbsp; {&nbsp;&nbsp; _issue= new Issue[NUM_ISSUE+1];&nbsp; }&nbsp;}实际上我们往往用Vector &nbsp;...&nbsp;&nbsp;<a href='http://www.cppblog.com/luyulaile/archive/2010/12/21/137138.html'>阅读全文</a><img src ="http://www.cppblog.com/luyulaile/aggbug/137138.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2010-12-21 19:57 <a href="http://www.cppblog.com/luyulaile/archive/2010/12/21/137138.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java 命名规范zt</title><link>http://www.cppblog.com/luyulaile/archive/2010/12/21/137128.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Tue, 21 Dec 2010 08:44:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2010/12/21/137128.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/137128.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2010/12/21/137128.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/137128.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/137128.html</trackback:ping><description><![CDATA[<p style="FONT-SIZE: 10pt">完全版java规范：<a href="http://www.huihoo.org/code/java_code_conventions.html#71">http://www.huihoo.org/code/java_code_conventions.html#71</a><br><br>&nbsp; <img border=0 alt="" src="http://www.cppblog.com/images/cppblog_com/luyulaile/QQ.gif" width=532 height=550></p>
<img src ="http://www.cppblog.com/luyulaile/aggbug/137128.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2010-12-21 16:44 <a href="http://www.cppblog.com/luyulaile/archive/2010/12/21/137128.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转载]Vector遍历</title><link>http://www.cppblog.com/luyulaile/archive/2010/01/26/106444.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Tue, 26 Jan 2010 01:08:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2010/01/26/106444.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/106444.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2010/01/26/106444.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/106444.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/106444.html</trackback:ping><description><![CDATA[<font color="#000080">import java.util.Iterator;<br />import java.util.Vector;<br /><br />public class VectorIterator implements Iterator{<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; private Vector v;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; private int currentIndex=0;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; public VectorIterator(){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; public VectorIterator(Vector v){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.v=v;<br />&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp; public boolean hasNext() {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(this.currentIndex&lt;this.v.size()){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("current index is :&nbsp;&nbsp; "+this.currentIndex);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("out of the bound ");<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br /><br />&nbsp;&nbsp;&nbsp;&nbsp; public Object next() {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this.v.get(this.currentIndex++);<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br /><br />&nbsp;&nbsp;&nbsp;&nbsp; public void remove() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.v.remove(this.currentIndex);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String[] args){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Vector v=new Vector();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; v.add(new String("aaa"));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; v.add(new String("bbb"));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; v.add(new String("ccc"));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //System.out.println(v);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Iterator iter=new VectorIterator(v);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(iter.hasNext()){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String str=(String)iter.next();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(str);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />}</font><img src ="http://www.cppblog.com/luyulaile/aggbug/106444.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2010-01-26 09:08 <a href="http://www.cppblog.com/luyulaile/archive/2010/01/26/106444.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java Vector排序及用法</title><link>http://www.cppblog.com/luyulaile/archive/2010/01/26/106439.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Mon, 25 Jan 2010 17:48:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2010/01/26/106439.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/106439.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2010/01/26/106439.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/106439.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/106439.html</trackback:ping><description><![CDATA[<p>temp=CustomerMaker.quickPassQueue.get(0);//获得首元素<br />&nbsp;&nbsp;CustomerMaker.quickPassQueue.remove(0);//删除首元素<br /><br />&nbsp;quickPassQueue.add();在末尾添加元素<br />----演示程序-----<br /><br />import java.util.*;<br />import java.io.*;<br /><br />public class test<br />{<br />Vector v = new Vector();<br /><br />public void testMethod(){<br />&nbsp; v.add("a");<br />&nbsp; v.add("b");<br />&nbsp; v.add("c");<br /><br />&nbsp; System.out.println("\n原来的vecotr");<br />&nbsp; for(int i=0;i&lt;v.size();i++){<br />&nbsp;&nbsp; System.out.println("\n"+" index is: "+ i + " element is: " +v.get(i));<br />&nbsp; }<br /><br />&nbsp; v.remove(0);<br /><br />&nbsp; System.out.println("\n去掉一个vector中第一个元素");<br />&nbsp; for(int i=0;i&lt;v.size();i++){<br />&nbsp;&nbsp; System.out.println("\n"+" index is: "+ i + " element is: " +v.get(i));<br />&nbsp; }<br />}<br /><br />public static void main(String[] args) <br />{<br />&nbsp; test t = new test();<br />&nbsp; t.testMethod();<br />&nbsp; System.out.println("Hello World!");<br />}<br />}<br />-----------------排序程序------------------------------<br />import java.util.*;</p>
<p>class MyCompare implements Comparator //实现Comparator，定义自己的比较方法<br />{<br />public int compare(Object o1, Object o2) {<br />Elem e1=(Elem)o1;<br />Elem e2=(Elem)o2;<br /><br />if(e1.get() &gt; e2.get())//这样比较是降序,如果把-1改成1就是升序.<br />{<br />&nbsp;&nbsp; return -1;<br />}<br />else if(e1.get()&lt;e2.get())<br />{<br />&nbsp;&nbsp; return 1;<br />}<br />else<br />{<br />&nbsp;&nbsp; return 0;<br />}<br />}<br />}</p>
<p>class Elem {<br />private int iVal;</p>
<p>public Elem(int i) {<br />&nbsp;&nbsp; this.iVal = i;<br />}</p>
<p>public int get() {<br />&nbsp;&nbsp; return this.iVal;<br />}<br />}</p>
<p>public class Vector1 {<br />public static void main(String[] args) {<br />&nbsp;&nbsp; List v = new Vector();<br />&nbsp;&nbsp; v.add(new Elem(1));<br />&nbsp;&nbsp; v.add(new Elem(22));<br />&nbsp;&nbsp; v.add(new Elem(3));<br />&nbsp;&nbsp; v.add(new Elem(14));<br />&nbsp;&nbsp; Comparator ct = new MyCompare();<br />&nbsp;&nbsp; Collections.sort(v, ct);<br />&nbsp;&nbsp; for (int i = 0; i &lt; v.size(); i++)<br />&nbsp;&nbsp;&nbsp; System.out.println(((Elem) v.get(i)).get());</p>
<p>}</p>
<p>}<br /></p><img src ="http://www.cppblog.com/luyulaile/aggbug/106439.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2010-01-26 01:48 <a href="http://www.cppblog.com/luyulaile/archive/2010/01/26/106439.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转载]使用 Java 编程语言实现线程 </title><link>http://www.cppblog.com/luyulaile/archive/2010/01/26/106437.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Mon, 25 Jan 2010 17:17:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2010/01/26/106437.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/106437.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2010/01/26/106437.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/106437.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/106437.html</trackback:ping><description><![CDATA[<p style="margin: 0cm 0cm 12pt" class="MsoNormal"><strong><span style="font-family: 宋体; color: #c00000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">使用</span></strong><strong><span style="font-family: Arial; color: #c00000; font-size: 9pt" lang="EN-US"> Java </span></strong><strong><span style="font-family: 宋体; color: #c00000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">编程语言实现线程</span></strong><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> <br /><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">Java</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">编程语言使多线程如此简单有效，以致于某些程序员说它实际上是自然的。尽管在</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> Java </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">中使用线程比在其他语言中要容易得多，仍然有一些概念需要掌握。要记住的一件重要的事情是</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> main() </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">函数也是一个线程，并可用来做有用的工作。程序员只有在需要多个线程时才需要创建新的线程。</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> <br /><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><strong><span style="font-family: Arial; font-size: 9pt" lang="EN-US">Thread </span></strong><strong><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">类</span></strong><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><br /><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">Thread </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">类是一个具体的类，即不是抽象类，该类封装了线程的行为。要创建一个线程，程序员必须创建一个从</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> Thread </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">类导出的新类。程序员必须覆盖</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> Thread </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">的</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> run() </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">函数来完成有用的工作。用户并不直接调用此函数；而是必须调用</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> Thread </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">的</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> start() </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">函数，该函数再调用</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> run()</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">。下面的代码说明了它的用法：</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><br /><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　创建两个新线程</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><o:p></o:p></span></p>
<div align="center">
<table style="border-bottom: #ffcc66 1pt outset; border-left: #ffcc66 1pt outset; width: 90%; background: #c9c9b8; border-top: #ffcc66 1pt outset; border-right: #ffcc66 1pt outset; mso-cellspacing: 1.5pt; mso-border-alt: outset #FFCC66 .75pt" class="MsoNormalTable" border="1" cellpadding="0" width="90%">
    <tbody>
        <tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes">
            <td style="border-bottom: #ffcc66 1pt inset; border-left: #ffcc66 1pt inset; padding-bottom: 0.75pt; background-color: transparent; padding-left: 0.75pt; padding-right: 0.75pt; border-top: #ffcc66 1pt inset; border-right: #ffcc66 1pt inset; padding-top: 0.75pt; mso-border-alt: inset #FFCC66 .75pt">
            <p style="margin: 0cm 0cm 0pt" class="MsoNormal"><span style="font-family: Arial; font-size: 9pt" lang="EN-US">import java.util.*;<br /><br />class TimePrinter extends Thread {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">int pauseTime;<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">String name;<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">public TimePrinter(int x, String n) {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">pauseTime = x;<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">name = n;<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">}<br /><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">public void run() {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">while(true) {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">try {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">System.out.println(name + ":" + new Date(System.currentTimeMillis()));<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">Thread.sleep(pauseTime);<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">} catch(Exception e) {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">System.out.println(e);<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">}<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">}<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">}<br /><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">static public void main(String args[]) {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">TimePrinter tp1 = new TimePrinter(1000, "Fast Guy");<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">tp1.start();<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">TimePrinter tp2 = new TimePrinter(3000, "Slow Guy");<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">tp2.start();<br /><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">}<br />}<o:p></o:p></span></p>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p style="margin: 0cm 0cm 12pt" class="MsoNormal"><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　在本例中，我们可以看到一个简单的程序，它按两个不同的时间间隔（</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">1 </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">秒和</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> 3 </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">秒）在屏幕上显示当前时间。这是通过创建两个新线程来完成的，包括</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> main() </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">共三个线程。但是，因为有时要作为线程运行的类可能已经是某个类层次的一部分，所以就不能再按这种机制创建线程。虽然在同一个类中可以实现任意数量的接口，但</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> Java </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">编程语言只允许一个类有一个父类。同时，某些程序员避免从</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> Thread </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">类导出，因为它强加了类层次。对于这种情况，就要</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> runnable </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">接口。</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><br /><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><strong><span style="font-family: Arial; font-size: 9pt" lang="EN-US">Runnable </span></strong><strong><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">接口</span></strong><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><br /><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　此接口只有一个函数，</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">run()</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">，此函数必须由实现了此接口的类实现。但是，就运行这个类而论，其语义与前一个示例稍有不同。我们可以用</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> runnable </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">接口改写前一个示例。（不同的部分用黑体表示。）</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> <br /><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　创建两个新线程而不强加类层次</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><o:p></o:p></span></p>
<div align="center">
<table style="border-bottom: #ffcc66 1pt outset; border-left: #ffcc66 1pt outset; width: 90%; background: #c9c9b8; border-top: #ffcc66 1pt outset; border-right: #ffcc66 1pt outset; mso-cellspacing: 1.5pt; mso-border-alt: outset #FFCC66 .75pt" class="MsoNormalTable" border="1" cellpadding="0" width="90%">
    <tbody>
        <tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes">
            <td style="border-bottom: #ffcc66 1pt inset; border-left: #ffcc66 1pt inset; padding-bottom: 0.75pt; background-color: transparent; padding-left: 0.75pt; padding-right: 0.75pt; border-top: #ffcc66 1pt inset; border-right: #ffcc66 1pt inset; padding-top: 0.75pt; mso-border-alt: inset #FFCC66 .75pt">
            <p style="margin: 0cm 0cm 0pt" class="MsoNormal"><span style="font-family: Arial; font-size: 9pt" lang="EN-US">import java.util.*;<br /><br />class TimePrinter implements Runnable {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">int pauseTime;<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">String name;<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">public TimePrinter(int x, String n) {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">pauseTime = x;<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">name = n;<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">}<br /><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">public void run() {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">while(true) {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">try {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">System.out.println(name + ":" + new Date(System.currentTimeMillis()));<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">Thread.sleep(pauseTime);<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">} catch(Exception e) {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">System.out.println(e);<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">}<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">}<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">}<br /><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">static public void main(String args[]) {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">Thread t1 = new Thread(new TimePrinter(1000, "Fast Guy"));<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">t1.start();<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">Thread t2 = new Thread(new TimePrinter(3000, "Slow Guy"));<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">t2.start();<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">}<br />}<o:p></o:p></span></p>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p style="margin: 0cm 0cm 12pt" class="MsoNormal"><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><br /><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　请注意，当使用</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> runnable </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">接口时，您不能直接创建所需类的对象并运行它；必须从</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> Thread </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">类的一个实例内部运行它。许多程序员更喜欢</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> runnable </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">接口，因为从</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> Thread </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">类继承会强加类层次。</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><br /><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><strong><span style="font-family: Arial; font-size: 9pt" lang="EN-US">synchronized </span></strong><strong><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">关键字</span></strong><strong><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><br /></span></strong><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　到目前为止，我们看到的示例都只是以非常简单的方式来利用线程。只有最小的数据流，而且不会出现两个线程访问同一个对象的情况。但是，在大多数有用的程序中，线程之间通常有信息流。试考虑一个金融应用程序，它有一个</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> Account </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">对象，如下例中所示：</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><br /><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　一个银行中的多项活动</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><o:p></o:p></span></p>
<div align="center">
<table style="border-bottom: #ffcc66 1pt outset; border-left: #ffcc66 1pt outset; width: 90%; background: #c9c9b8; border-top: #ffcc66 1pt outset; border-right: #ffcc66 1pt outset; mso-cellspacing: 1.5pt; mso-border-alt: outset #FFCC66 .75pt" class="MsoNormalTable" border="1" cellpadding="0" width="90%">
    <tbody>
        <tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes">
            <td style="border-bottom: #ffcc66 1pt inset; border-left: #ffcc66 1pt inset; padding-bottom: 0.75pt; background-color: transparent; padding-left: 0.75pt; padding-right: 0.75pt; border-top: #ffcc66 1pt inset; border-right: #ffcc66 1pt inset; padding-top: 0.75pt; mso-border-alt: inset #FFCC66 .75pt">
            <p style="margin: 0cm 0cm 0pt" class="MsoNormal"><span style="font-family: Arial; font-size: 9pt" lang="EN-US">public class Account {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">String holderName;<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">float amount;<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">public Account(String name, float amt) {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">holderName = name;<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">amount = amt;<br />}<br /><br />public void deposit(float amt) {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">amount += amt;<br />}<br /><br />public void withdraw(float amt) {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">amount -= amt;<br />}<br /><br />public float checkBalance() {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">return amount;<br />}<br />}<o:p></o:p></span></p>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p style="margin: 0cm 0cm 12pt" class="MsoNormal"><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　在此代码样例中潜伏着一个错误。如果此类用于单线程应用程序，不会有任何问题。但是，在多线程应用程序的情况中，不同的线程就有可能同时访问同一个</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> Account </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">对象，比如说一个联合帐户的所有者在不同的</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> ATM </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">上同时进行访问。在这种情况下，存入和支出就可能以这样的方式发生：一个事务被另一个事务覆盖。这种情况将是灾难性的。但是，</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">Java </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">编程语言提供了一种简单的机制来防止发生这种覆盖。每个对象在运行时都有一个关联的锁。这个锁可通过为方法添加关键字</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> synchronized </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">来获得。这样，修订过的</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> Account </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">对象（如下所示）将不会遭受像数据损坏这样的错误：</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><br /><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　对一个银行中的多项活动进行同步处理</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><o:p></o:p></span></p>
<div align="center">
<table style="border-bottom: #ffcc66 1pt outset; border-left: #ffcc66 1pt outset; width: 90%; background: #c9c9b8; border-top: #ffcc66 1pt outset; border-right: #ffcc66 1pt outset; mso-cellspacing: 1.5pt; mso-border-alt: outset #FFCC66 .75pt" class="MsoNormalTable" border="1" cellpadding="0" width="90%">
    <tbody>
        <tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes">
            <td style="border-bottom: #ffcc66 1pt inset; border-left: #ffcc66 1pt inset; padding-bottom: 0.75pt; background-color: transparent; padding-left: 0.75pt; padding-right: 0.75pt; border-top: #ffcc66 1pt inset; border-right: #ffcc66 1pt inset; padding-top: 0.75pt; mso-border-alt: inset #FFCC66 .75pt">
            <p style="margin: 0cm 0cm 0pt" class="MsoNormal"><span style="font-family: Arial; font-size: 9pt" lang="EN-US">public class Account {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">String holderName;<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">float amount;<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">public Account(String name, float amt) {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">holderName = name;<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">amount = amt;<br />}<br /><br />public synchronized void deposit(float amt) {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">amount += amt;<br />}<br /><br />public synchronized void withdraw(float amt) {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">amount -= amt;<br />}<br /><br />public float checkBalance() {<br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">return amount;<br />}<br />}<o:p></o:p></span></p>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p style="margin: 0cm 0cm 0pt" class="MsoNormal"><span style="font-family: Arial; font-size: 9pt" lang="EN-US"><br /></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">　　</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US">deposit() </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">和</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> withdraw() </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">函数都需要这个锁来进行操作，所以当一个函数运行时，另一个函数就被阻塞。请注意，</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> checkBalance() </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">未作更改，它严格是一个读函数。因为</span><span style="font-family: Arial; font-size: 9pt" lang="EN-US"> checkBalance() </span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">未作同步处理，所以任何其他方法都不会阻塞它，它也不会阻塞任何其他方法，不管那些方法是否进行了同步处理</span></p><img src ="http://www.cppblog.com/luyulaile/aggbug/106437.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2010-01-26 01:17 <a href="http://www.cppblog.com/luyulaile/archive/2010/01/26/106437.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>