﻿<?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>BlogJava-JafeLee-随笔分类-Java</title><link>http://www.blogjava.net/JafeLee/category/22866.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 10 Dec 2007 15:31:55 GMT</lastBuildDate><pubDate>Mon, 10 Dec 2007 15:31:55 GMT</pubDate><ttl>60</ttl><item><title>Java 正则表达式 (2) -- Metacharacters &amp; Character Classes</title><link>http://www.blogjava.net/JafeLee/archive/2007/12/10/166494.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Mon, 10 Dec 2007 01:58:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/12/10/166494.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/166494.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/12/10/166494.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/166494.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/166494.html</trackback:ping><description><![CDATA[1、元字符(metacharacters):元字符是指那些可以代表特殊含义的字符，java.util.regex支持的元字符包括<font color="#0000ff"><code>([{\^-$|]})?*+.<font color="#000000">。注意，在某些时候这些特殊的字符并不一定代表特殊含义，例如 - 在[a-c]中是元字符，但在 a-c 则不表示特殊含义。不过上面没列出的字符则一定不会代表特殊含义。<br /><br />2、可以对元字符进行转义，主要有两种方法：<br /></font></code></font><ul><li>在元字符前面加一个反斜杠 (backslash）\ <br /></li><li>用 \Q 和 \E包含起来，例如 \? == \Q?\E</li></ul>3、Character Classes ：这里的class并不是Java中的class，在正则表达式中，一个character class是由一系列包含在方括号中的字符组成，它能在输入的一系列字符串中匹配一个字符，例如[a-d]匹配a, b, c, d,中的任何一个字符。再看下面一个例子（摘自<a href="http://java.sun.com/docs/books/tutorial/essential/regex/char_classes.html">java.sun.com</a>）<br /><div align="center"><br /></div><table align="center" border="1"><tbody><tr><td colspan="2"><b><center>Character Classes</center></b></td></tr><tr><td><code>[abc]</code></td><td>
a, b, or c (simple class)
</td></tr><tr><td><code>[^abc]</code></td><td>          
Any character except a, b, or c (negation)
</td></tr><tr><td><code>[a-zA-Z] </code></td><td>          
a through z, or A through Z, inclusive (range)
</td></tr><tr><td><code>[a-d[m-p]] </code></td><td>          
a through d, or m through p: [a-dm-p] (union)
</td></tr><tr><td><code> [a-z&amp;&amp;[def]]</code></td><td>          
d, e, or f (intersection)
</td></tr><tr><td><code>[a-z&amp;&amp;[^bc]]</code></td><td>          
a through z, except for b and c: [ad-z] (subtraction)
</td></tr><tr><td><code>[a-z&amp;&amp;[^m-p]] </code></td><td>          
a through z, and not m through p: [a-lq-z] (subtraction)</td></tr></tbody></table><br />4、Character Classes 之间的运算：从上面的表格也可以看出 character classes包含了几个跟集合操作很相似的操作：例如negation（补）、union(并）、intersection(交）、subtraction(差）。由上面的表格也可以看出两个Character Classes之间的运算只需要简单地将一个Character Classes嵌入到另一个Character Classes并使用正确的操作符就可以了。<br /><br />5、Predefined Character Classes：java.util.regex.* 包含了一些预先定义的 Character Classes,在实际中可以很方便地运用：（摘自 <a href="http://java.sun.com/docs/books/tutorial/essential/regex/pre_char_classes.html">java.sun.com</a> )<br /><br /><div align="center"><table border="1"><tbody><tr><td colspan="2"><center><b>Predefined Character Classes</b></center></td></tr><tr><td width="15"><code>. </code></td><td>
Any character (may or may not match line terminators)
</td></tr><tr><td><code>\d</code></td><td>         
A digit: <code>[0-9]</code></td></tr><tr><td><code>\D</code></td><td>         
A non-digit: <code>[^0-9]</code></td></tr><tr><td><code>\s</code></td><td>         
A whitespace character: <code>[ \t\n\x0B\f\r]</code></td></tr><tr><td><code>\S</code></td><td>         
A non-whitespace character: <code>[^\s]</code></td></tr><tr><td><code>\w</code></td><td>         
A word character: <code>[a-zA-Z_0-9]</code></td></tr><tr><td><code>\W</code></td><td>         A non-word character: <code>[^\w]</code></td></tr></tbody></table><div align="left"><br /></div></div><img src ="http://www.blogjava.net/JafeLee/aggbug/166494.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-12-10 09:58 <a href="http://www.blogjava.net/JafeLee/archive/2007/12/10/166494.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java 正则表达式 (1)  --  java.util.regex.*  介绍</title><link>http://www.blogjava.net/JafeLee/archive/2007/12/09/166489.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Sun, 09 Dec 2007 11:47:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/12/09/166489.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/166489.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/12/09/166489.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/166489.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/166489.html</trackback:ping><description><![CDATA[1、Java 1.4之后的版本引进了一个用于处理正则表达式的包 java.util.regex.*; 该包主要包含三个类：<br /><ul><li><font color="#0000ff">Pattern</font> : 用来表示一个经过编译处理后的正则表达式。通俗一点来说，就是用一个类来表示一个正则表达式，这个类是从正则表达式构造得到的。这个类并没有public constructor, 如果想得到一个这个类的一个对象则必须调用该类的public static方法：public static Pattern compile(String regex)或者 public static Pattern compile(String regex,int flags)。这两个方法返回一个Pattern型的对象。</li><li><font color="#0000ff">Matcher</font> : 解释Pattern并执行匹配、查找工作的类，跟Pattern类一样，这个类也没有定义public constructor，要想获得一个Matcher对象必须调用Pattern类的方法 public Matcher matcher(CharSequence input) 来得到。</li><li><font color="#0000ff">PatternSyntaxException</font> : 一个unchecked exception。当遇到不符和Java正则表达式的语法的时候程序就会抛出这个异常。</li></ul>2、一个例子（摘自<a href="http://java.sun.com/docs/books/tutorial/essential/regex/examples/RegexTestHarness.java">java.sun.com</a> ）<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> regex;<br /></span><span style="color: rgb(0, 0, 255);"><br />import</span><span style="color: rgb(0, 0, 0);"> java.io.Console;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.regex.Pattern;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.regex.Matcher;</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> RegexTestHarness {<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String [] args) {<br />        Console console </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> System.console();<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (console </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">) {<br />            System.err.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">No console.</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />            System.exit(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">) {<br />            Pattern pattern </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Pattern.compile(console.readLine(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%nEnter your regex: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">));<br />            Matcher matcher </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> pattern.matcher(console.readLine(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Enter input string to search: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">));<br />            </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> found </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);"> (matcher.find()) {<br />                console.format(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">I found the text \</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">s\</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">starting at </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                        </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">index %d and ending at index %d. %n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, matcher.group(), matcher.start(), matcher.end());<br />                found </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br />            }<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">found)<br />                console.format(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">No match found.%n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        }<br />    }<br />}<br /></span></div>注：由于这个例子使用了JDK 1.6后才有的方法：System.console(),所以这个例子在eclipse和netbeans都不能正常运行。只有在命令行下才能正确运行。如果想在eclipse和nb下运行，好像可以用System.out/in来代替System.console。<br /><img src ="http://www.blogjava.net/JafeLee/aggbug/166489.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-12-09 19:47 <a href="http://www.blogjava.net/JafeLee/archive/2007/12/09/166489.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java Thread (1)</title><link>http://www.blogjava.net/JafeLee/archive/2007/09/12/144519.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Wed, 12 Sep 2007 13:32:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/09/12/144519.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/144519.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/09/12/144519.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/144519.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/144519.html</trackback:ping><description><![CDATA[ 1、开启一个新线程的方法，归结起来不外乎有两种：<br /><ul><li>继承类java.lang.Thread ，覆盖其中的方法 run(); 实例化该子类，例如：<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> MyThread </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Thread <br />{<br />   </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> run() <br />  {<br />      </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> do some work</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">   }<br />}<br /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> code to use MyThread</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> MyThread().start();<br /></span></div></li><li><span style="color: rgb(0, 0, 0);">定义一个实现接口 java.lang.Runnable; 的类，实现该接口的方法run(); 然后将该类的一个实例作为实参传给Thread的构造方法。例如：<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> MyThread2 </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);"> Runnable <br />{<br />   </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> run() <br />  {<br />      </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> do some work</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">   }<br />}<br /><br /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> code to use MyThread2</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">Thread t </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Thread(MyThread2);<br />t.start();<br /></span></div></span></li><li><span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 0);">无论用哪种方法最终得到的都是一个Thread的object，当调用方法start()时，系统将创建一个新的线程来执行run() 方法，这个新的线程将一直运行知道run方法退出。在新线程开始运行的同时，原来的线程也将继续执行start()之后的代码。</span></span></li></ul>2、一个线程的生命周期：每个线程的生命周期(lifecycle)一共有六个状态(state)，这六个状态用枚举类型Thread.State的六个枚举常量来表示。分别是：<br /><ul><li>NEW : 线程已经被创建，但是start()方法还没被调用，所有的线程都是从这个状态开始的</li><li>RUNABLE : 该线程已经在JVM上执行了，但可能还在等待操作系统种的其他资源，也就是说要么现在就在运行，要么只要操作系统调度到该线程时立即就可以执行。</li><li>BLOCKED ： 线程并没有运行因为它必须等待直到获得一个锁以便进入一个synchronized的方法或者块。</li><li>WAITING : 线程并没有运行因为它已经调用了方法 <tt>Object.wait()</tt><a name="javanut5-CHP-5-ITERM-11137"></a><a name="javanut5-CHP-5-ITERM-11138"></a><a name="javanut5-CHP-5-ITERM-11139"></a>或者 <tt>Thread.join( )</tt>.</li><li><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">TIMED_WAITING</span></span><a name="javanut5-CHP-5-ITERM-11140"> ：线程并没有运行因为它已经调用了Thread.sleep()或者调用了使用timeout值的</a></span><a name="javanut5-CHP-5-ITERM-11140"><tt>Object.wait()</tt></a><a name="javanut5-CHP-5-ITERM-11137"></a><a name="javanut5-CHP-5-ITERM-11138"></a><a name="javanut5-CHP-5-ITERM-11139"></a>或者 <tt>Thread.join( )</tt>.</li><li><a name="javanut5-CHP-5-ITERM-11140"><span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont">TERMINATED</span></span></span></a><a name="javanut5-CHP-5-ITERM-11141"> : 线程已经完成执行任务，即run()方法要么正常退出，要么抛出一个异常。</a><br /><span class="docPubcolor"><a name="javanut5-CHP-5-ITERM-11140"></a></span></li></ul>3、线程优先级：线程可以有不同优先级，Thread定义了三个优先级常量：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> MIN_PRIORITY </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">; </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">最小优先级</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> NORM_PRIORITY </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">; </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">默认的优先级</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> MAX_PRIORITY </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">; </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">最大优先级</span></div>一个线程的优先级可以是1到10中任何一个整数（包括1和10).方法 <tt>void setPriority(int newPriority) 可以用来设置线程有限级。关于优先级需要注意的是，调度器总是选择优先级最高的线程来执行，但是java的线程优先级是和操作系统相关的，例如windows XP系统有7个优先级，java中的某些优先级会对应到xp的7个优先级中去。但是SUN 的linux版本的JVM上所有线程的优先级都是NORM_PRIORITY。在使用优先级的同时也要避免饿死的情况出现。<br /><br />4、处理 uncaught exceptions:<br />当一个线程的run()方法执行完毕或者run()方法执行return语句时，线程正常中止。当run方法抛出一个unchecked异常时（注意：run方法是不能抛出checked异常的），线程也会中止并且打印出异常信息。如果你想捕获处理该异常，则必须为该线程添加一个uncaught exception handler来执行。例如：<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> uncaughtexception;<br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> MyThread </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);"> Runnable<br />{<br />    @Override<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> run()<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">throw</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> UnsupportedOperationException();<br />    }<br />}<br /></span></div><br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> uncaughtexception;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> TestMyThread<br />{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String [] args)<br />    {<br />        Thread aThread </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Thread(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> MyThread());<br />        <br />        aThread.setUncaughtExceptionHandler(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Thread.UncaughtExceptionHandler()<br />        {<br />            @Override<br />            </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> uncaughtException(Thread t, Throwable e)<br />            {<br />                System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Hello</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />            }<br />        });<br />        <br />        aThread.start();<br />    }<br />}<br /></span></div>运行结果: Hello<br /></tt><img src ="http://www.blogjava.net/JafeLee/aggbug/144519.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-09-12 21:32 <a href="http://www.blogjava.net/JafeLee/archive/2007/09/12/144519.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java Enumeration (枚举类型) (5) -- Value-specific class body</title><link>http://www.blogjava.net/JafeLee/archive/2007/09/09/143793.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Sun, 09 Sep 2007 07:29:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/09/09/143793.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/143793.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/09/09/143793.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/143793.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/143793.html</trackback:ping><description><![CDATA[什么是Value-specific class body呢？简而言之，就是不同的枚举常量 (enumeration constant)拥有自己的类定义，先看一个例子（这个例子的行为可以用更优雅的代码来实现，用它只是为了说明问题）<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_152723" onclick="this.style.display='none'; Code_Closed_Text_152723.style.display='none'; Code_Open_Image_152723.style.display='inline'; Code_Open_Text_152723.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_152723" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_152723.style.display='none'; Code_Closed_Image_152723.style.display='inline'; Code_Closed_Text_152723.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_152723" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">ArithmeticOp.java</span><span id="Code_Open_Text_152723" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> valspe;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">enum</span><span style="color: rgb(0, 0, 0);"> ArthmeticOp<br />{<br />    ADD(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br />    {<br />        <br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">You will never be able to refer to this method<br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">outside this block</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> onlyForAdd()<br />        {<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">hey</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        }<br />        <br />        </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> showOperator()<br />        {<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        }<br />        <br />        </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> compute(</span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> x, </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> y)<br />        {<br />            </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> (x</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">y);<br />        }<br />    },<br />    MINUS(</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">)<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> showOperator()<br />        {<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        }<br />        <br />        </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> compute(</span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> x, </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> y)<br />        {<br />            </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> (x</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">y);<br />        }<br />    },<br />    TIMES(</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">)<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> showOperator()<br />        {<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        }<br />        <br />        </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> compute(</span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> x, </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> y)<br />        {<br />            </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> (x</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">y);<br />        }<br />    },<br />    DIVIDE(</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">)<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> showOperator()<br />        {<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        }<br />        <br />        </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> compute(</span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> x, </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> y)<br />        {<br />            </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> (x</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">y);<br />        }<br />    };<br />    <br />    ArthmeticOp(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> flag)<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.flag </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> flag;<br />    }<br />    <br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> showOperator()<br />    {<br />        System.out.println(flag);<br />    }<br />    <br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">Define a abstract method.You must implement this method<br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> in all the enumeration constants, or you will get a <br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> compiler-time error</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> compute(</span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> x, </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> y);<br />    <br />    </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> flag; </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> a useless field, just for demo</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">}<br /></span></span></div><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_144430" onclick="this.style.display='none'; Code_Closed_Text_144430.style.display='none'; Code_Open_Image_144430.style.display='inline'; Code_Open_Text_144430.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_144430" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_144430.style.display='none'; Code_Closed_Image_144430.style.display='inline'; Code_Closed_Text_144430.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_144430" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">TestOperator.java</span><span id="Code_Open_Text_144430" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> valspe;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> TestOpeator<br />{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String [] args)<br />    {<br />        ArthmeticOp add </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ArthmeticOp.ADD;<br />        System.out.println(add.compute(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">));<br />        add.showOperator();<br />        ArthmeticOp times </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ArthmeticOp.TIMES;<br />        times.showOperator();<br />    }<br />}<br /></span></span></div><br /><ol><li>定义每一个 value-specific class body （不知如何翻译~~)将会创建一个该枚举类型的匿名子类（anonymous subclass), 对应的枚举常量将会唯一的指向该子类。这里需要注意的是，尽管枚举类型不能被extends，但是它却可以拥有匿名子类，从这个意义上来说，枚举类型并不是一个严格的final类。</li><li>匿名子类就预示着这些子类没有自己的constructor，你也不能在你的代码中引用一些匿名子类自己拥有的方法或域（见上面例子ADD中的justForAdd方法），这是因为每个枚举类型的枚举常量的枚举类型都是该枚举类型的。可能这句话我表述的不好。举个例子，设有类 A, B是A的子类，B有一个自己的方法 bMethod()。在代码中，我先new一个B的实例b，然后再declare一个A的变量a,并让a指向b(a = b)，这时，尽管a，b指向同一个对象，但你可以b.bMethod()却不能够a.bMethod()。</li><li>枚举类型可以定义abstract方法，但你必须至少定义一个枚举常量来实现这些abstract方法，否则将会产生编译错误。</li><li>由2可以看出，定义value-specific class body实际上就是实现枚举类型中的abstract方法或者override枚举类型种存在的方法。<br /></li></ol><img src ="http://www.blogjava.net/JafeLee/aggbug/143793.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-09-09 15:29 <a href="http://www.blogjava.net/JafeLee/archive/2007/09/09/143793.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java Enumeration (枚举类型) (3) -- 自定义类体(class body)</title><link>http://www.blogjava.net/JafeLee/archive/2007/09/08/143614.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Sat, 08 Sep 2007 07:39:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/09/08/143614.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/143614.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/09/08/143614.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/143614.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/143614.html</trackback:ping><description><![CDATA[枚举类型其实是一个有限制的类，很多类的语法都可以用在枚举上面上，例如自定义域、方法、构造方法等。先看下面一个例子：<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> custom;<br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">enum</span><span style="color: rgb(0, 0, 0);"> Prefix<br />{<br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> These are the values of this enumerated type.<br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Each one is followed by constructor arguments in parentheses.<br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> The values are separated from each other by commas, and the<br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> list of values is terminated with a semicolon to separate it from<br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> the class body that follows.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    MILLI(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">m</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, .</span><span style="color: rgb(0, 0, 0);">001</span><span style="color: rgb(0, 0, 0);">), <br />    CENTI(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">c</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, .</span><span style="color: rgb(0, 0, 0);">01</span><span style="color: rgb(0, 0, 0);">), <br />    DECI(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">d</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">), <br />    DECA(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">D</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">10.0</span><span style="color: rgb(0, 0, 0);">), <br />    HECTA(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">h</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">100.0</span><span style="color: rgb(0, 0, 0);">), <br />    KILO(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">k</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">1000.0</span><span style="color: rgb(0, 0, 0);">);  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Note semicolon<br /><br />                                                                                                                                                                                                                        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> semicolon<br /><br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> This is the constructor invoked for each value above.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> Prefix(String abbrev, </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> multiplier)<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.abbrev </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> abbrev;<br />        </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.multiplier </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> multiplier;<br />    }<br />    <br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">Another constructor</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> Prefix(String abbrev)<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.abbrev </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> abbrev;<br />        </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.multiplier </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> .</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />    }<br />    <br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> These are the private fields set by the constructor</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> String abbrev;<br />    </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> multiplier;<br /><br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> These are accessor methods for the fields. They are instance methods<br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> of each value of the enumerated type.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String abbrev()<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> abbrev;<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> multiplier()<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> multiplier;<br />    }<br />}<br /></span></div>需注意，一个枚举类型可以有多个构造器。<br /><br /><img src ="http://www.blogjava.net/JafeLee/aggbug/143614.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-09-08 15:39 <a href="http://www.blogjava.net/JafeLee/archive/2007/09/08/143614.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java Enumeration (枚举类型) (2) -- switch语句</title><link>http://www.blogjava.net/JafeLee/archive/2007/09/08/143609.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Sat, 08 Sep 2007 06:43:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/09/08/143609.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/143609.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/09/08/143609.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/143609.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/143609.html</trackback:ping><description><![CDATA[当枚举类型用在switch语句中时，语法有一点点特别。看例子：<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> other;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> basic.Day;<br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> SwitchTest<br />{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String [] args)<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> FRIDAY </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;<br />        System.out.println(FRIDAY);<br />        <br />        Day today </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Day.SATURDAY;<br />        </span><span style="color: rgb(0, 0, 255);">switch</span><span style="color: rgb(0, 0, 0);">(today)<br />        {<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> FRIDAY:<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Today is </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> today.toString().toLowerCase());<br />            </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> SATURDAY:<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Today is </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> today.toString().toLowerCase());<br />            </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> SUNDAY:<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Today is </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> today.toString().toLowerCase());<br />            </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> MONDAY:<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Today is </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> today.toString().toLowerCase());<br />            </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> TUESDAT:<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Today is </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> today.toString().toLowerCase());<br />            </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> WEDNESDAY:<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Today is </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> today.toString().toLowerCase());<br />            </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> THURSDAY:<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Today is </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> today.toString().toLowerCase());<br />            </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">default</span><span style="color: rgb(0, 0, 0);">:<br />            System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">otherday</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        }<br />    }<br />}<br /></span></div>注意在case标签中，Day不出现，即case Day.FRIDAY是不合法的。而在其他地方出现时则必须用Day.FRIDAY<br /><img src ="http://www.blogjava.net/JafeLee/aggbug/143609.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-09-08 14:43 <a href="http://www.blogjava.net/JafeLee/archive/2007/09/08/143609.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java Enumeration (枚举类型) (1)  -- 基本概念</title><link>http://www.blogjava.net/JafeLee/archive/2007/09/08/143578.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Sat, 08 Sep 2007 06:13:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/09/08/143578.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/143578.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/09/08/143578.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/143578.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/143578.html</trackback:ping><description><![CDATA[
		<font size="3">Java 5.0新引进了一种类型：枚举类型。昨晚看了一下，语法还是比较复杂的，至少比C的枚举要复杂的很多，不过功能也强大了很多。具体语法请参见 JLS 8.9<br />1、定义一个功能简单的枚举类型，更定义一个简单的类很相似，例如</font>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<font size="3">
						<span style="color: rgb(0, 0, 255);">package</span>
						<span style="color: rgb(0, 0, 0);"> basic;<br /></span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">enum</span>
						<span style="color: rgb(0, 0, 0);"> Day<br />{<br />    MONDAY, TUESDAT, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY<br />}<br /></span>
				</font>
		</div>
		<ul>
				<li>
						<font size="3">跟类定义一样，枚举类型可以单独放在一个文件里，当一个枚举类型用public修饰时，它对其他包可见，否则只对同一个包中的类可见，这和类定义是一样的。</font>
				</li>
				<li>
						<font size="3">标识符 MONDAY, TUESDAY等就称为枚举常量（enumeration constants）</font>
				</li>
				<li>
						<font size="3">每一个枚举常量被隐式的声明成Day的一个public、static成员，而且其类型为Day，亦就是说这些常量是self-typed的<br /></font>
				</li>
		</ul>
		<font size="3">2、下面的定义也是合法的：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> basic;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">enum</span><span style="color: rgb(0, 0, 0);"> Day<br />{<br />    MONDAY, TUESDAT, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY,<br />}<br /></span></div>或<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> basic;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">enum</span><span style="color: rgb(0, 0, 0);"> Day<br />{<br />    MONDAY, TUESDAT, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY,;<br />}<br /></span></div>或<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> basic;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">enum</span><span style="color: rgb(0, 0, 0);"> Day<br />{<br />    MONDAY, TUESDAT, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;<br />}<br /></span></div>但是当枚举类型有其他定义时，则分号;是必须的<br />3、声明、使用一个枚举类型：<br />（1）在同一个包中：<br /><br /></font>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<font size="3">
						<img id="Code_Closed_Image_105143" onclick="this.style.display='none'; Code_Closed_Text_105143.style.display='none'; Code_Open_Image_105143.style.display='inline'; Code_Open_Text_105143.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" />
						<span id="Code_Closed_Text_105143" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">BasicMainClass.java</span>
				</font>
				<img id="Code_Open_Image_105143" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_105143.style.display='none'; Code_Closed_Image_105143.style.display='inline'; Code_Closed_Text_105143.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" />
				<span id="Code_Open_Text_105143" style="display: none;">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="color: rgb(0, 0, 255);">package</span>
						<span style="color: rgb(0, 0, 0);"> basic;<br /><br /></span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">class</span>
						<span style="color: rgb(0, 0, 0);"> BasicMainClass<br />{<br />    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">static</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">void</span>
						<span style="color: rgb(0, 0, 0);"> main(String args[])<br />    {<br />        Day today </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> Day.SATURDAY;<br />        <br />        System.out.println(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">Today is </span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 0);">+</span>
						<span style="color: rgb(0, 0, 0);"> today.toString().toLowerCase());<br />    }<br />}</span>
				</span>
		</div>
		<font size="3">
				<br />（2）在不同包中：<br /><br /></font>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<font size="3">
						<img id="Code_Closed_Image_105213" onclick="this.style.display='none'; Code_Closed_Text_105213.style.display='none'; Code_Open_Image_105213.style.display='inline'; Code_Open_Text_105213.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" />
						<span id="Code_Closed_Text_105213" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">OtherMainClass.java</span>
				</font>
				<img id="Code_Open_Image_105213" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_105213.style.display='none'; Code_Closed_Image_105213.style.display='inline'; Code_Closed_Text_105213.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" />
				<span id="Code_Open_Text_105213" style="display: none;">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="color: rgb(0, 0, 255);">package</span>
						<span style="color: rgb(0, 0, 0);"> other;<br /></span>
						<span style="color: rgb(0, 0, 255);">import</span>
						<span style="color: rgb(0, 0, 0);"> basic.Day;<br /></span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">class</span>
						<span style="color: rgb(0, 0, 0);"> OtherMainClass<br />{<br />    </span>
						<span style="color: rgb(0, 0, 255);">public</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">static</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">void</span>
						<span style="color: rgb(0, 0, 0);"> main(String [] args)<br />    {<br />        Day today </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> Day.SATURDAY;<br />        System.out.println(</span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">Today is </span>
						<span style="color: rgb(0, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 0);">+</span>
						<span style="color: rgb(0, 0, 0);"> today.toString().toLowerCase());<br />    }<br />}<br /></span>
				</span>
		</div>
		<font size="3">
				<br />4、枚举类型的性质：（摘自o'relly 出版的 Java in A Nutshell 5th)<br /><br /></font>
		<ul>
				<li>
						<p class="docList">
								<font face="Tahoma" size="3">Enumerated types<font color="#ff0000"> have no public <a name="javanut5-CHP-4-ITERM-10429"></a>constructor. The only instances of an 
enumerated type are those declared by the enum.</font></font>
						</p>
				</li>
				<li>
						<p class="docList">
								<font face="Tahoma" size="3">Enums <font color="#ff0000">are not Cloneable,</font> so copies of the existing 
instances cannot be created.</font>
						</p>
				</li>
				<li>
						<p class="docList">
								<font face="Tahoma" size="3">
										<font color="#ff0000">Enums implement java.io.Serializable </font>so they can be 
serialized, but the Java serialization mechanism handles them specially to 
ensure that no new instances are ever created.</font>
						</p>
				</li>
				<li>
						<p class="docList">
								<font face="Tahoma" size="3">
										<a name="javanut5-CHP-4-ITERM-10430">
										</a>Instances of an 
enumerated type are immutable: each enum value retains its identity. (We'll see 
later in this chapter that you can add your own fields and methods to an 
enumerated type, which means that you can create enumerated values that have 
mutable portions. This is not recommended, but does not affect the basic 
identity of each value.)</font>
						</p>
				</li>
				<li>
						<p class="docList">
								<font color="#000000" face="Tahoma" size="3">
										<font color="#ff0000">Instances of an enumerated type are stored in public static 
final fields of the type itself</font>. Because these fields are final, 
they cannot be overwritten with inappropriate values: you can't assign the 
DownloadStatus.ERROR value to the DownloadStatus.DONE</font>
								<font color="#000000">
								</font>
								<font color="#000000">
								</font>
								<font color="#000000" size="3">
								</font>
								<font color="#000000" size="3"> field, 
for example.</font>
						</p>
				</li>
				<li>
						<p class="docList">
								<font face="Tahoma" size="3">By convention, the values of enumerated types are written using 
all capital letters, just as other static final fields are.</font>
						</p>
				</li>
				<li>
						<p class="docList">
								<font color="#ff0000" face="Tahoma" size="3">Because there is a strictly limited set of distinct enumerated 
values, it is always safe to compare enum values using the = =<a name="javanut5-CHP-4-ITERM-10431"></a> operator instead of calling the 
equals()<a name="javanut5-CHP-4-ITERM-10432"></a> method.</font>
						</p>
				</li>
				<li>
						<p class="docList">
								<font face="Tahoma" size="3">Enumerated types do have a working equals( ) method, 
however. <font color="#ff0000">The method uses <font size="4"><b><i>= =final</i></b></font>so that 
it cannot be overridden. </font>This working equals( ) method allows 
enumerated values to be used as members of collections such as Set, 
List, and Map.</font>
								<font size="3">
								</font>
								<font size="3"> internally and is </font>
						</p>
				</li>
				<li>
						<p class="docList">
								<font face="Tahoma" size="3">Enumerated types have a working hashCode()<a name="javanut5-CHP-4-ITERM-10433"></a> method consistent with their equals( 
)equals(), hashCode( ) is final. 
It allows enumerated values to be used with classes like 
java.util.HashMap.</font>
								<font size="3"> method. Like </font>
						</p>
				</li>
				<li>
						<p class="docList">
								<font face="Tahoma" size="3">Enumerated types implement java.lang.Comparable<a name="javanut5-CHP-4-ITERM-10434"></a>, and the compareTo()<a name="javanut5-CHP-4-ITERM-10435"></a> method orders enumerated values in the 
order in which they appear in the enum declaration.</font>
						</p>
				</li>
				<li>
						<p class="docList">
								<font color="#000000" face="Tahoma" size="3">Enumerated types include a working toString( )<a name="javanut5-CHP-4-ITERM-10436"></a> method that returns the name of the 
enumerated value. For example, <font color="#ff0000">DownloadStatus.DONE.toString( ) returns 
the string "DONE" by default</font>. <font color="#ff0000">This method is not fina</font>l, and 
enum types can provide a custom implementation if they choose.</font>
						</p>
				</li>
				<li>
						<p class="docList">
								<font color="#000000" face="Tahoma" size="3">Enumerated types provide<font color="#ff0000"> a static valueOf( )<a name="javanut5-CHP-4-ITERM-10437"></a> method </font>that does the opposite of the 
default <br />toString( ) method. For example, 
<font color="#ff0000">DownloadStatus.valueOf("DONE") would return 
DownloadStatus.DONE.</font></font>
						</p>
				</li>
				<li>
						<p class="docList">
								<font face="Tahoma" size="3">Enumerated types define <font color="#ff0000">a final instance method named<font color="#000000"><i><font color="#0000ff">ordinal()<a name="javanut5-CHP-4-ITERM-10438"></a><a name="javanut5-CHP-4-ITERM-10439"></a></font></i></font><font color="#0000ff"></font>that returns an integer for each enumerated 
value.</font> The ordinal of an enumerated value represents its position (starting at 
zero) in the list of value names in the enum declaration. You do not 
typically need to use the ordinal( ) method, but it is used by a number 
of enum-related facilities, as described later in the chapter.</font>
						</p>
				</li>
				<li>
						<p class="docList">
								<font face="Tahoma" size="3">Each enumerated type defines a static method named <font color="#0000ff"><i>values( 
)</i></font> that returns an array of enumerated values of that type. This array 
contains the complete set of values, in the order they were declared, and is 
useful for iterating through the complete set of possible values. Because arrays 
are mutable, the values( ) method always returns a newly created and 
initialized array.</font>
						</p>
				</li>
				<li>
						<p class="docList">
								<font color="#000000" face="Tahoma" size="3">Enumerated types are subclasses of <font color="#ff0000">java.lang.Enum</font>, <a name="javanut5-CHP-4-ITERM-10440"></a><a name="javanut5-CHP-4-ITERM-10441"></a>which 
is new in Java 5.0. (Enum is not itself an enumerated type.) <font color="#ff0000">You cannot 
produce an enumerated type by manually extending the Enum class</font>, and it 
is a compilation error to attempt this. The only way to define an enumerated 
type is with the enum keyword.</font>
						</p>
				</li>
				<li>
						<p class="docList">
								<font color="#000000" face="Tahoma" size="3">
										<font color="#ff0000">It is not possible to <a name="javanut5-CHP-4-ITERM-10442"></a><a name="javanut5-CHP-4-ITERM-10443"></a>extend an enumerated type</font>. Enumerated types 
are effectively final, <font color="#ff0000">but the final keyword is neither 
required nor permitted in their declarations</font>. Because enums are effectively 
final, <font color="#ff0000">they may not be abstract</font>.</font>
						</p>
				</li>
				<li>
						<p class="docList">
								<font color="#000000" face="Tahoma" size="3">Like classes, enumerated types may implement <a name="javanut5-CHP-4-ITERM-10444"></a>one or more interfaces. </font>
						</p>
				</li>
		</ul>
		<font size="3">
				<br />
		</font>
<img src ="http://www.blogjava.net/JafeLee/aggbug/143578.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-09-08 14:13 <a href="http://www.blogjava.net/JafeLee/archive/2007/09/08/143578.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java 单元测试</title><link>http://www.blogjava.net/JafeLee/archive/2007/08/22/138699.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Wed, 22 Aug 2007 13:00:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/08/22/138699.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/138699.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/08/22/138699.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/138699.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/138699.html</trackback:ping><description><![CDATA[由于来自ibm developworks,转载要提交申请，就不转内容了。。。<br /><br />1、单元测试利器 JUnit 4<br /><a href="http://www.ibm.com/developerworks/cn/java/j-lo-junit4/">http://www.ibm.com/developerworks/cn/java/j-lo-junit4/</a><br /><br />2、JUnit 4抢先看<br /><a href="http://www.ibm.com/developerworks/cn/java/j-junit4.html">http://www.ibm.com/developerworks/cn/java/j-junit4.html</a><br /><br />3、深入探索 JUnit4（这个似乎要先注册的）<br /><a href="http://www.ibm.com/developerworks/cn/edu/j-dw-java-junit4.html">http://www.ibm.com/developerworks/cn/edu/j-dw-java-junit4.html</a><br /><br />4、<font size="3">TestNG 使 Java 单元测试轻而易举<br /><a href="http://www.ibm.com/developerworks/cn/java/j-testng/">http://www.ibm.com/developerworks/cn/java/j-testng/</a><br /><br />5、</font><font size="3">追求代码质量: JUnit 4 与 TestNG 的对比<br /><a href="http://www.ibm.com/developerworks/cn/java/j-cq08296/">http://www.ibm.com/developerworks/cn/java/j-cq08296/</a><br /><br /></font><img src ="http://www.blogjava.net/JafeLee/aggbug/138699.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-08-22 21:00 <a href="http://www.blogjava.net/JafeLee/archive/2007/08/22/138699.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java点滴 (3)</title><link>http://www.blogjava.net/JafeLee/archive/2007/07/23/131890.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Mon, 23 Jul 2007 07:47:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/07/23/131890.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/131890.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/07/23/131890.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/131890.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/131890.html</trackback:ping><description><![CDATA[1、AWT事件继承层次<br /><img src="http://www.blogjava.net/images/blogjava_net/jafelee/awtevent.JPG" alt="awtevent.JPG" border="0" height="551" width="512" /><br /><br />2、常用AWT事件类型列表<br /><font color="#0000ff">ActionEvent                          KeyEvent
<br />AdjustmentEvent                      MouseEvent
<br />FocusEvent                           MouseWheelEvent
<br />ItemEvent                            WindowEvent</font><br /><br />用于监听这些事件的接口：<br /><pre><font color="#0000ff">ActionListener                  MouseMotionListener<br />AdjustmentListener              MouseWheelListener<br />FocusListener                   WindowListener<br />ItemListener                    WindowFocusListener<br />KeyListener                     WindowStateListener<br />MouseListener</font><br /><br />常用的适配器类：<br /><font color="#0000ff">FocusAdapter                    MouseMotionAdapter<br />KeyAdapter                      WindowAdapter<br />MouseAdapter</font><br /><br /></pre>3、事件监听原理：<br />接收事件的类必须实现监听接口，这个类要用事件源注册，然后，接受所需要的事件对象，并通过监听器接口中的方法对事件作出相应的处理<br /><br />4、所有AWT事件源都支持多点传送（multicast)模型，即同一个事件可以发送给多个监听器对象。当时API不能保证一个给定事件源注册的一组监听器传送事件的次序，因此不要编写依赖传送次序的程序逻辑。<br /><img src ="http://www.blogjava.net/JafeLee/aggbug/131890.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-07-23 15:47 <a href="http://www.blogjava.net/JafeLee/archive/2007/07/23/131890.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java继承</title><link>http://www.blogjava.net/JafeLee/archive/2007/07/22/131188.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Sun, 22 Jul 2007 08:40:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/07/22/131188.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/131188.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/07/22/131188.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/131188.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/131188.html</trackback:ping><description><![CDATA[
		<font size="2">1、关键字super有两个用途：一是调用超类的方法，而是调用超类的构造器。<br />
super不是对一个对象的引用，不能将super赋给另一个对象变量，它只是一个指示编译器调用超类方法的特有关键字。super在构造器中的作用：<br /></font>
		<font size="2">
				<div class="dp-highlighter">
						<ol>
								<font>
										<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
												<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
												<span style="color: rgb(0, 0, 255);">public</span>
												<span style="color: rgb(0, 0, 0);"> Manager(String n, </span>
												<span style="color: rgb(0, 0, 255);">double</span>
												<span style="color: rgb(0, 0, 0);"> s, </span>
												<span style="color: rgb(0, 0, 255);">int</span>
												<span style="color: rgb(0, 0, 0);"> year, </span>
												<span style="color: rgb(0, 0, 255);">int</span>
												<span style="color: rgb(0, 0, 0);"> month, </span>
												<span style="color: rgb(0, 0, 255);">int</span>
												<span style="color: rgb(0, 0, 0);"> day)    <br />{      <br />  </span>
												<span style="color: rgb(0, 0, 255);">super</span>
												<span style="color: rgb(0, 0, 0);">(n, s, year, month, day);    <br />  bonus </span>
												<span style="color: rgb(0, 0, 0);">=</span>
												<span style="color: rgb(0, 0, 0);"> </span>
												<span style="color: rgb(0, 0, 0);">0</span>
												<span style="color: rgb(0, 0, 0);">;    <br />}    <br /></span>
										</div>
										<br />
								</font>
								<font>
								</font>
						</ol>
				</div>
		</font>
		<font size="2">
    
    由于构造器不能范围Employee类的私有域（具体例子见本文最后面PersonTest.java），所以必须利用Employee类的构造器对这部分私有域进行初始化<font color="#ff0000">子</font></font>
		<font size="2">，我们可以通过super实现对超类构造器的调用。<font color="#ff0000">使用super调用构造器的语句必须是</font></font>
		<font size="2">
				<font color="#ff0000">类构造器的第一条语句</font>。如果之类的构造器没有显示的调用超类的构造器，这将自动调用超类默认（没有参数）的构造器。如果超类没有不带参数的构造器，并且在子类的构造器中没有显示地调用超类的其它构造器，则Java编译器将报告错误，这个规则对与abstract base class 也是成立的。<br /><br />
    2、关键字this有两个用途：一是引用隐式参数，二是调用该类的其它的构造器。在调用构造器时和super很相似，都只能作为另一个构造器的第一条语句出现。<br /><br />3、多态。先看例子：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">Manager boss </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Manager(<img src="http://www.blogjava.net/images/dot.gif" />);<br />Employee [] staff </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Employee[</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">];<br />staff[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> boss;</span></div>在这个例子中，变量staff[0]和boss引用同一个对象.但编译器将staff[0]看待成Employee对象.这意味着可以这样调用:<br />boss.setBonus(5000);<br />但不能这样调用<br />staff[0].setBonus(5000);//error，这样会引起一个编译错误<br />这是因为staff[0]声明的类型是Employee，而setBonus不是Empoyee类的方法。<br /><br />4、动态绑定：<br /></font>
		<ul>
				<li>
						<font size="2">方法的名字和参数列表被称为方法的签名（signature).如果在子类中定义了一个与超类签名相同的方法，那么子类中的这个方法就覆盖了超类中的这个同签名的方法。但是，返回类型不是签名的一部分，因此在覆盖方法的时候，一定要保证返回类型的兼容性。在JDK 5.0以前的版本中，要求返回类型必须是一样的。而现在允许子类将覆盖方法返回类型定义为原返回类型的子类型。例如，假设Employee类有：<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Employee getBuddy(){<img src="http://www.blogjava.net/images/dot.gif" />}</span></div>在后面的子类Manager中，可以按照如下所示的方法覆盖这个方法：<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Manager getBuddy(){<img src="http://www.blogjava.net/images/dot.gif" />} </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">OK in JDK 5.0</span></div></font>
				</li>
				<li>
						<font size="2">
								<span style="color: rgb(0, 128, 0);">
										<font color="#000000">如果方法是private, static, final或者是构造器，那么编译器将可以准确地知道应该调用那个方法。我们将这种调用方式称为静态绑定。但是，子类不能继承父类private （方法,更谈不上覆盖）不能覆盖final修饰的方法，但可一覆盖static方法</font>
								</span>
						</font>
				</li>
				<li>
						<font size="2">
								<span style="color: rgb(0, 128, 0);">
										<font color="#000000">覆盖一个方法的时候，子类方法不能低于超类方法的可见性。特别是如果超类得方法是public，那么子类方法一定要声明为public。<br /></font>
								</span>
						</font>
				</li>
		</ul>
<img src ="http://www.blogjava.net/JafeLee/aggbug/131188.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-07-22 16:40 <a href="http://www.blogjava.net/JafeLee/archive/2007/07/22/131188.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java 点滴  (2)</title><link>http://www.blogjava.net/JafeLee/archive/2007/07/21/120165.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Sat, 21 Jul 2007 01:55:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/07/21/120165.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/120165.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/07/21/120165.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/120165.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/120165.html</trackback:ping><description><![CDATA[1、关于import:<br />（1） 使用 * 一次只能import 一个package的类，例如想要 import 包java.util中内容不能用 <font color="#0000ff">import java.*.*;</font> 也不能企图通过这种方式：<font color="#0000ff">import java.*; util.Date date = new Date();<font color="#000000">来引用<br /><br />（2）static imports: 从JDK 5.0开始，可以用static方法来import 某个类的静态方法和静态常量。例如：<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><img id="Code_Closed_Image_130851" onclick="this.style.display='none'; Code_Closed_Text_130851.style.display='none'; Code_Open_Image_130851.style.display='inline'; Code_Open_Text_130851.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_130851" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_130851.style.display='none'; Code_Closed_Image_130851.style.display='inline'; Code_Closed_Text_130851.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_130851" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">ImportTest.java</span><span id="Code_Open_Text_130851" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> java.lang.System.</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> ImportTest<br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String [] args)<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">    {<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">        Date date </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Date();<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">        out.println(date);<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    }<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">}</span></span></div><br />static imports 的好处：例如：</font>sqrt(pow(x, 2) + pow(y, 2))</font>比<font color="#0000ff">Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2))</font>  ； <font color="#0000ff"> if (d.get(DAY_OF_WEEK) == MONDAY)</font> 比 <font color="#0000ff">if (d.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY)</font> 看起来要整洁，可读性也丝毫不会受影响~~<br /><br />2、块作用域：（参考资料：《Java2 核心技术》）<br />（1）块（即符合语句）是指由一对花括号括起来的若干条简单的Java语句。块确定了变量的作用域。一个块可以嵌套在另一个块中。例如<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><img id="Code_Closed_Image_201839" onclick="this.style.display='none'; Code_Closed_Text_201839.style.display='none'; Code_Open_Image_201839.style.display='inline'; Code_Open_Text_201839.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_201839" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_201839.style.display='none'; Code_Closed_Image_201839.style.display='inline'; Code_Closed_Text_201839.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_201839" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">TestBlock.java</span><span id="Code_Open_Text_201839" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> TestBlock<br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String [] args)<br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);">    {<br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> n;<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">        {<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> k;<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">        }</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">k 的作用域到此结束</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">    }<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">}</span></span></div>注意k的作用域，在花括号外是不能再引用k的。<br />（2）不能在嵌套的两个块中声明同名变量。例如下面的代码编译时是通不过的：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><img id="Code_Closed_Image_202445" onclick="this.style.display='none'; Code_Closed_Text_202445.style.display='none'; Code_Open_Image_202445.style.display='inline'; Code_Open_Text_202445.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_202445" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_202445.style.display='none'; Code_Closed_Image_202445.style.display='inline'; Code_Closed_Text_202445.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_202445" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">TestBlock.java</span><span id="Code_Open_Text_202445" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> TestBlock<br /><img id="Codehighlighter1_23_164_Open_Image" onclick="this.style.display='none'; Codehighlighter1_23_164_Open_Text.style.display='none'; Codehighlighter1_23_164_Closed_Image.style.display='inline'; Codehighlighter1_23_164_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_23_164_Closed_Image" style="display: none;" onclick="this.style.display='none'; Codehighlighter1_23_164_Closed_Text.style.display='none'; Codehighlighter1_23_164_Open_Image.style.display='inline'; Codehighlighter1_23_164_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span><span id="Codehighlighter1_23_164_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_23_164_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String [] args)<br /><img id="Codehighlighter1_73_162_Open_Image" onclick="this.style.display='none'; Codehighlighter1_73_162_Open_Text.style.display='none'; Codehighlighter1_73_162_Closed_Image.style.display='inline'; Codehighlighter1_73_162_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_73_162_Closed_Image" style="display: none;" onclick="this.style.display='none'; Codehighlighter1_73_162_Closed_Text.style.display='none'; Codehighlighter1_73_162_Open_Image.style.display='inline'; Codehighlighter1_73_162_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span><span id="Codehighlighter1_73_162_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_73_162_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> n;<br /><img id="Codehighlighter1_98_156_Open_Image" onclick="this.style.display='none'; Codehighlighter1_98_156_Open_Text.style.display='none'; Codehighlighter1_98_156_Closed_Image.style.display='inline'; Codehighlighter1_98_156_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_98_156_Closed_Image" style="display: none;" onclick="this.style.display='none'; Codehighlighter1_98_156_Closed_Text.style.display='none'; Codehighlighter1_98_156_Open_Image.style.display='inline'; Codehighlighter1_98_156_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />        </span><span id="Codehighlighter1_98_156_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_98_156_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />            </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> k;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />            </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> n;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">n不能再这里声明</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" /></span><span style="color: rgb(0, 0, 0);">        }</span></span><span style="color: rgb(0, 0, 0);"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span></span><span style="color: rgb(0, 0, 0);"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span></span></div><br />但是这样是可以的：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><img id="Code_Closed_Image_202507" onclick="this.style.display='none'; Code_Closed_Text_202507.style.display='none'; Code_Open_Image_202507.style.display='inline'; Code_Open_Text_202507.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_202507" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_202507.style.display='none'; Code_Closed_Image_202507.style.display='inline'; Code_Closed_Text_202507.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_202507" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">TestBlock.java</span><span id="Code_Open_Text_202507" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> TestBlock<br /><img id="Codehighlighter1_23_154_Open_Image" onclick="this.style.display='none'; Codehighlighter1_23_154_Open_Text.style.display='none'; Codehighlighter1_23_154_Closed_Image.style.display='inline'; Codehighlighter1_23_154_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_23_154_Closed_Image" style="display: none;" onclick="this.style.display='none'; Codehighlighter1_23_154_Closed_Text.style.display='none'; Codehighlighter1_23_154_Open_Image.style.display='inline'; Codehighlighter1_23_154_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span><span id="Codehighlighter1_23_154_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_23_154_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String [] args)<br /><img id="Codehighlighter1_73_152_Open_Image" onclick="this.style.display='none'; Codehighlighter1_73_152_Open_Text.style.display='none'; Codehighlighter1_73_152_Closed_Image.style.display='inline'; Codehighlighter1_73_152_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_73_152_Closed_Image" style="display: none;" onclick="this.style.display='none'; Codehighlighter1_73_152_Closed_Text.style.display='none'; Codehighlighter1_73_152_Open_Image.style.display='inline'; Codehighlighter1_73_152_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span><span id="Codehighlighter1_73_152_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_73_152_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img id="Codehighlighter1_83_131_Open_Image" onclick="this.style.display='none'; Codehighlighter1_83_131_Open_Text.style.display='none'; Codehighlighter1_83_131_Closed_Image.style.display='inline'; Codehighlighter1_83_131_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_83_131_Closed_Image" style="display: none;" onclick="this.style.display='none'; Codehighlighter1_83_131_Closed_Text.style.display='none'; Codehighlighter1_83_131_Open_Image.style.display='inline'; Codehighlighter1_83_131_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />        </span><span id="Codehighlighter1_83_131_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><img src="http://www.blogjava.net/images/dot.gif" /></span><span id="Codehighlighter1_83_131_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />            </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> k;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />            </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> n;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />        }</span></span><span style="color: rgb(0, 0, 0);"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align="top" />        </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> n;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span></span><span style="color: rgb(0, 0, 0);"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span></span></div><br />3、使用内部类(inner class)的动机：<br /><ul><li>内部类方法可以访问该类定义所在的作用域的数据，包括私有的数据</li><li>内部类可以对同一个包中的其它类隐藏起来</li><li>当想要定义一个回调函数且不想编写大量代码时使用匿名（anonymous)内部类比较便捷。（?)</li></ul>4、只有内部类可以是私有类，常规类只能具有包的可见性或公有的可见性<br /><br />5、JFrame 与JPane类的继承层次结构<br /><img src="http://www.blogjava.net/images/blogjava_net/jafelee/jframe.JPG" alt="jframe.JPG" align="middle" border="0" height="747" width="512" /><br /><br />6、有时候JFrame程序退出时为什么要调用setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)?<br />这是因为在包含多个程序框架的程序中，不能因为用户关闭了其中一个框架就让程序退出。在默认情况下用户关闭窗口时只是将框架隐藏了起来，而程序并没有终止。<br /><br />7、封装了用户系统的各个元素的颜色的SystemColor类中的系统颜色和它们的含义：<br /><p></p><table cellpadding="5" cellspacing="0" frame="hsides" height="791" rules="none" width="718"><caption><h5 class="docTableTitle">System Colors</h5></caption><colgroup><col width="247" /><col width="302" /></colgroup><thead></thead><tbody><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>desktop</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Background color of desktop</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>activeCaption</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Background color for captions</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>activeCaptionText</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Text color for captions</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>activeCaptionBorder</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Border color for caption text</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>inactiveCaption</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Background color for inactive captions</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>inactiveCaptionText</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Text color for inactive captions</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>inactiveCaptionBorder</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Border color for inactive captions</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>window</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Background for windows</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>windowBorder</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Color of window border frame</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>windowText</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Text color inside windows</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>menu</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Background for menus</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>menuText</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Text color for menus</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>text</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Background color for text</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>textText</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Text color for text</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>textInactiveText</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Text color for inactive controls</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>textHighlight</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Background color for highlighted text</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>textHighlightText</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Text color for highlighted text</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>control</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Background color for controls</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>controlText</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Text color for controls</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>controlLtHighlight</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Light highlight color for controls</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>controlHighlight</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Highlight color for controls</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>controlShadow</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Shadow color for controls</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>controlDkShadow</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Dark shadow color for controls</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>scrollbar</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Background color for scrollbars</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>info</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Background color for spot-help text</p></td></tr><tr><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>infoText</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText">Text color for spot-help 
text</p></td></tr></tbody></table><img src ="http://www.blogjava.net/JafeLee/aggbug/120165.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-07-21 09:55 <a href="http://www.blogjava.net/JafeLee/archive/2007/07/21/120165.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java Interface</title><link>http://www.blogjava.net/JafeLee/archive/2007/05/26/119852.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Fri, 25 May 2007 16:16:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/05/26/119852.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/119852.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/05/26/119852.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/119852.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/119852.html</trackback:ping><description><![CDATA[1、一个Interface的方所有法访问权限（visibility)自动被声明为public,确却的说，一个Interface的所有方法只能是public的，你可以显式声明一个方法是public（不推荐),但是不能声明它是private或protected.但是当一个类实现某个接口，定义接口的方法时，必须且只能声明为public，否则编译将通不过。<br /><br />2、接口不能实现方法（implement method),只能声明。接口可以只定义常量但不声明任何方法。<br /><br />3、Interface不能有实例域（instance fields)或静态方法（static method),但可以定义常量(define constants),常量自动设为public static final，可以通过类命直接引用常量，例如<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">ImplementClass.z</span></div>可以通过接口命和常量名直接访问常量：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">FirstInterface.z</span></div><br />4、一个<font color="#000000">非抽象</font>类<font color="#ff0000">（注意是非抽象类！）</font>实现一个接口时，必须实现接口的所有方法，抽象类则不必实现所有方法。<br /><br />5、不能使用new操作符实例化一个接口，但可以声明一个接口变量，该变量必须引用（refer to)一个实现该接口的类的对象。可以使用 instanceof 检查一个对象是否实现了某个特定的接口。例如：<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(anObject </span><span style="color: rgb(0, 0, 255);">instanceof</span><span style="color: rgb(0, 0, 0);"> Comparable){<img src="http://www.blogjava.net/images/dot.gif" />}</span></div><br /><br />6、接口可以被另一个接口继承（但是final好像不能修饰interface，编译通不过，以后慢慢研究~~)<br /><br />7、标记接口（tagging interface, marker interface)没有方法，使用它的唯一目的是可以用instanceof 进行类型检查（Horstmann说了，不鼓励用这种技术,^_^）<br /><br />8、<font size="3">方法的名字和参数列表被称为方法的签名（signature)，实现一个接口以为着要用完全相同的签名实现每个方法。因此实现接口方法时，一定要保证返回类型的兼容性。</font><font size="3">允许实现类的实现方法返回类型定义为原返回类型的子类型</font>。这个跟继承中子类覆盖父类方法很相似。<br /><br />8、例子：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><img id="Code_Closed_Image_000952" onclick="this.style.display='none'; Code_Closed_Text_000952.style.display='none'; Code_Open_Image_000952.style.display='inline'; Code_Open_Text_000952.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_000952" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_000952.style.display='none'; Code_Closed_Image_000952.style.display='inline'; Code_Closed_Text_000952.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_000952" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">FirstInterface.java</span><span id="Code_Open_Text_000952" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">interface</span><span style="color: rgb(0, 0, 0);"> FirstInterface<br />{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">20</span><span style="color: rgb(0, 0, 0);">;   </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">int x; 是不允许的</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> y </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">21</span><span style="color: rgb(0, 0, 0);">;   </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">private int y=21; 或protected int y=22;均为非法声明</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> z </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">22</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> u </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">23</span><span style="color: rgb(0, 0, 0);">;  <br />    </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> foobar();<br />}</span></span></div><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><img id="Code_Closed_Image_001149" onclick="this.style.display='none'; Code_Closed_Text_001149.style.display='none'; Code_Open_Image_001149.style.display='inline'; Code_Open_Text_001149.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_001149" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_001149.style.display='none'; Code_Closed_Image_001149.style.display='inline'; Code_Closed_Text_001149.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_001149" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">SecondInterface.java</span><span id="Code_Open_Text_001149" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);">1</span> <span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">2</span> <span style="color: rgb(0, 128, 0);">* 继承了FirstInterface的所有常量和方法<br /></span><span style="color: rgb(0, 128, 128);">3</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"> <br /></span><span style="color: rgb(0, 128, 128);">4</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">interface</span><span style="color: rgb(0, 0, 0);"> SecondInterface </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> FirstInterface<br /></span><span style="color: rgb(0, 128, 128);">5</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">6</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> squad(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> x);<br /></span><span style="color: rgb(0, 128, 128);">7</span> <span style="color: rgb(0, 0, 0);">}</span></span></div><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><img id="Code_Closed_Image_001443" onclick="this.style.display='none'; Code_Closed_Text_001443.style.display='none'; Code_Open_Image_001443.style.display='inline'; Code_Open_Text_001443.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_001443" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_001443.style.display='none'; Code_Closed_Image_001443.style.display='inline'; Code_Closed_Text_001443.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_001443" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">ImplementClass.java</span><span id="Code_Open_Text_001443" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);">* 类ImplementClass 必须实现FirstInterface和<br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 128, 0);">* SecondInterface的所有方法<br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> ImplementClass </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);"> SecondInterface<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> foobar()<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">    {<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">        System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">I love you!</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">    }<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> squad(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> x)<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">    {<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> x</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x;<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">    }<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">}</span></span></div><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><img id="Code_Closed_Image_001506" onclick="this.style.display='none'; Code_Closed_Text_001506.style.display='none'; Code_Open_Image_001506.style.display='inline'; Code_Open_Text_001506.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_001506" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_001506.style.display='none'; Code_Closed_Image_001506.style.display='inline'; Code_Closed_Text_001506.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_001506" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">Main.java</span><span id="Code_Open_Text_001506" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Main<br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String [] args)<br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);">    {<br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);">        FirstInterface ic;<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">        ic  </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> ImplementClass();<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">        SecondInterface sic </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> ImplementClass();<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">        System.out.println(ic.x);<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">        System.out.println(sic.y);<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">        System.out.println(ImplementClass.z);<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">        ic.foobar();<br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">        sic.foobar();<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">    }<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">}</span></span></div><br />运行结果：<br />20<br />21<br />22<br />I love you!<br />I love you!<br />9、匿名内部类(anonymous inner class):<br />注意一个特别的例子<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_225020" onclick="this.style.display='none'; Code_Closed_Text_225020.style.display='none'; Code_Open_Image_225020.style.display='inline'; Code_Open_Text_225020.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_225020" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_225020.style.display='none'; Code_Closed_Image_225020.style.display='inline'; Code_Closed_Text_225020.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_225020" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">AnonymousInnerClass.java</span><span id="Code_Open_Text_225020" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.Comparator;<br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> AnonymousInnerClass<br />{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String [] args)<br />    {<br />         Comparator</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">String</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"> sizeOrder </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Comparator</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">String</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">() <br />         {<br />             </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> compare(String s1, String s2) <br />             {<br />                 </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> s1.length() </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);"> s2.length() </span><span style="color: rgb(0, 0, 0);">?</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"> : s1.length() </span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"> s2.length() </span><span style="color: rgb(0, 0, 0);">?</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"> : s1.compareTo(s2);<br />             }<br />         }; <br />         System.out.println(sizeOrder.compare(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Jafe</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Lee</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">));<br />    }<br />}</span></span></div>而java.util.Comparator的定义为<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_225149" onclick="this.style.display='none'; Code_Closed_Text_225149.style.display='none'; Code_Open_Image_225149.style.display='inline'; Code_Open_Text_225149.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_225149" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_225149.style.display='none'; Code_Closed_Image_225149.style.display='inline'; Code_Closed_Text_225149.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_225149" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">Comparator.java</span><span id="Code_Open_Text_225149" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);"><br /> * @(#)Comparator.java    1.26 06/04/21<br /> *<br /> * Copyright 2006 Sun Microsystems, Inc. All rights reserved.<br /> * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.<br /> </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> java.util;<br /><br /></span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br /> * A comparison function, which imposes a &lt;i&gt;total ordering&lt;/i&gt; on some<br /> * collection of objects.  Comparators can be passed to a sort method (such<br /> * as {</span><span style="color: rgb(128, 128, 128);">@link</span><span style="color: rgb(0, 128, 0);"> Collections#sort(List,Comparator) Collections.sort} or {</span><span style="color: rgb(128, 128, 128);">@link</span><span style="color: rgb(0, 128, 0);"><br /> * Arrays#sort(Object[],Comparator) Arrays.sort}) to allow precise control<br /> * over the sort order.  Comparators can also be used to control the order of<br /> * certain data structures (such as {</span><span style="color: rgb(128, 128, 128);">@link</span><span style="color: rgb(0, 128, 0);"> SortedSet sorted sets} or {</span><span style="color: rgb(128, 128, 128);">@link</span><span style="color: rgb(0, 128, 0);"><br /> * SortedMap sorted maps}), or to provide an ordering for collections of<br /> * objects that don't have a {</span><span style="color: rgb(128, 128, 128);">@link</span><span style="color: rgb(0, 128, 0);"> Comparable natural ordering}.&lt;p&gt;<br /> *<br /> * The ordering imposed by a comparator &lt;tt&gt;c&lt;/tt&gt; on a set of elements<br /> * &lt;tt&gt;S&lt;/tt&gt; is said to be &lt;i&gt;consistent with equals&lt;/i&gt; if and only if<br /> * &lt;tt&gt;c.compare(e1, e2)==0&lt;/tt&gt; has the same boolean value as<br /> * &lt;tt&gt;e1.equals(e2)&lt;/tt&gt; for every &lt;tt&gt;e1&lt;/tt&gt; and &lt;tt&gt;e2&lt;/tt&gt; in<br /> * &lt;tt&gt;S&lt;/tt&gt;.&lt;p&gt;<br /> *<br /> * Caution should be exercised when using a comparator capable of imposing an<br /> * ordering inconsistent with equals to order a sorted set (or sorted map).<br /> * Suppose a sorted set (or sorted map) with an explicit comparator &lt;tt&gt;c&lt;/tt&gt;<br /> * is used with elements (or keys) drawn from a set &lt;tt&gt;S&lt;/tt&gt;.  If the<br /> * ordering imposed by &lt;tt&gt;c&lt;/tt&gt; on &lt;tt&gt;S&lt;/tt&gt; is inconsistent with equals,<br /> * the sorted set (or sorted map) will behave "strangely."  In particular the<br /> * sorted set (or sorted map) will violate the general contract for set (or<br /> * map), which is defined in terms of &lt;tt&gt;equals&lt;/tt&gt;.&lt;p&gt;<br /> *<br /> * For example, suppose one adds two elements {</span><span style="color: rgb(128, 128, 128);">@code</span><span style="color: rgb(0, 128, 0);"> a} and {</span><span style="color: rgb(128, 128, 128);">@code</span><span style="color: rgb(0, 128, 0);"> b} such that<br /> * {</span><span style="color: rgb(128, 128, 128);">@code</span><span style="color: rgb(0, 128, 0);"> (a.equals(b) &amp;&amp; c.compare(a, b) != 0)}<br /> * to an empty {</span><span style="color: rgb(128, 128, 128);">@code</span><span style="color: rgb(0, 128, 0);"> TreeSet} with comparator {</span><span style="color: rgb(128, 128, 128);">@code</span><span style="color: rgb(0, 128, 0);"> c}.<br /> * The second {</span><span style="color: rgb(128, 128, 128);">@code</span><span style="color: rgb(0, 128, 0);"> add} operation will return<br /> * true (and the size of the tree set will increase) because {</span><span style="color: rgb(128, 128, 128);">@code</span><span style="color: rgb(0, 128, 0);"> a} and<br /> * {</span><span style="color: rgb(128, 128, 128);">@code</span><span style="color: rgb(0, 128, 0);"> b} are not equivalent from the tree set's perspective, even though<br /> * this is contrary to the specification of the<br /> * {</span><span style="color: rgb(128, 128, 128);">@link</span><span style="color: rgb(0, 128, 0);"> Set#add Set.add} method.&lt;p&gt;<br /> *<br /> * Note: It is generally a good idea for comparators to also implement<br /> * &lt;tt&gt;java.io.Serializable&lt;/tt&gt;, as they may be used as ordering methods in<br /> * serializable data structures (like {</span><span style="color: rgb(128, 128, 128);">@link</span><span style="color: rgb(0, 128, 0);"> TreeSet}, {</span><span style="color: rgb(128, 128, 128);">@link</span><span style="color: rgb(0, 128, 0);"> TreeMap}).  In<br /> * order for the data structure to serialize successfully, the comparator (if<br /> * provided) must implement &lt;tt&gt;Serializable&lt;/tt&gt;.&lt;p&gt;<br /> *<br /> * For the mathematically inclined, the &lt;i&gt;relation&lt;/i&gt; that defines the<br /> * &lt;i&gt;imposed ordering&lt;/i&gt; that a given comparator &lt;tt&gt;c&lt;/tt&gt; imposes on a<br /> * given set of objects &lt;tt&gt;S&lt;/tt&gt; is:&lt;pre&gt;<br /> *       {(x, y) such that c.compare(x, y) &amp;lt;= 0}.<br /> * &lt;/pre&gt; The &lt;i&gt;quotient&lt;/i&gt; for this total order is:&lt;pre&gt;<br /> *       {(x, y) such that c.compare(x, y) == 0}.<br /> * &lt;/pre&gt;<br /> *<br /> * It follows immediately from the contract for &lt;tt&gt;compare&lt;/tt&gt; that the<br /> * quotient is an &lt;i&gt;equivalence relation&lt;/i&gt; on &lt;tt&gt;S&lt;/tt&gt;, and that the<br /> * imposed ordering is a &lt;i&gt;total order&lt;/i&gt; on &lt;tt&gt;S&lt;/tt&gt;.  When we say that<br /> * the ordering imposed by &lt;tt&gt;c&lt;/tt&gt; on &lt;tt&gt;S&lt;/tt&gt; is &lt;i&gt;consistent with<br /> * equals&lt;/i&gt;, we mean that the quotient for the ordering is the equivalence<br /> * relation defined by the objects' {</span><span style="color: rgb(128, 128, 128);">@link</span><span style="color: rgb(0, 128, 0);"> Object#equals(Object)<br /> * equals(Object)} method(s):&lt;pre&gt;<br /> *     {(x, y) such that x.equals(y)}. &lt;/pre&gt;&lt;p&gt;<br /> *<br /> * This interface is a member of the<br /> * &lt;a href="{@</span><span style="color: rgb(128, 128, 128);">docRoot</span><span style="color: rgb(0, 128, 0);">}/../technotes/guides/collections/index.html"&gt;<br /> * Java Collections Framework&lt;/a&gt;.<br /> *<br /> * </span><span style="color: rgb(128, 128, 128);">@param</span><span style="color: rgb(0, 128, 0);"> &lt;T&gt; the type of objects that may be compared by this comparator<br /> *<br /> * </span><span style="color: rgb(128, 128, 128);">@author</span><span style="color: rgb(0, 128, 0);">  Josh Bloch<br /> * </span><span style="color: rgb(128, 128, 128);">@author</span><span style="color: rgb(0, 128, 0);">  Neal Gafter<br /> * </span><span style="color: rgb(128, 128, 128);">@version</span><span style="color: rgb(0, 128, 0);"> 1.26, 04/21/06<br /> * </span><span style="color: rgb(128, 128, 128);">@see</span><span style="color: rgb(0, 128, 0);"> Comparable<br /> * </span><span style="color: rgb(128, 128, 128);">@see</span><span style="color: rgb(0, 128, 0);"> java.io.Serializable<br /> * </span><span style="color: rgb(128, 128, 128);">@since</span><span style="color: rgb(0, 128, 0);"> 1.2<br /> </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">interface</span><span style="color: rgb(0, 0, 0);"> Comparator</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"> {<br />    </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />     * Compares its two arguments for order.  Returns a negative integer,<br />     * zero, or a positive integer as the first argument is less than, equal<br />     * to, or greater than the second.&lt;p&gt;<br />     *<br />     * In the foregoing description, the notation<br />     * &lt;tt&gt;sgn(&lt;/tt&gt;&lt;i&gt;expression&lt;/i&gt;&lt;tt&gt;)&lt;/tt&gt; designates the mathematical<br />     * &lt;i&gt;signum&lt;/i&gt; function, which is defined to return one of &lt;tt&gt;-1&lt;/tt&gt;,<br />     * &lt;tt&gt;0&lt;/tt&gt;, or &lt;tt&gt;1&lt;/tt&gt; according to whether the value of<br />     * &lt;i&gt;expression&lt;/i&gt; is negative, zero or positive.&lt;p&gt;<br />     *<br />     * The implementor must ensure that &lt;tt&gt;sgn(compare(x, y)) ==<br />     * -sgn(compare(y, x))&lt;/tt&gt; for all &lt;tt&gt;x&lt;/tt&gt; and &lt;tt&gt;y&lt;/tt&gt;.  (This<br />     * implies that &lt;tt&gt;compare(x, y)&lt;/tt&gt; must throw an exception if and only<br />     * if &lt;tt&gt;compare(y, x)&lt;/tt&gt; throws an exception.)&lt;p&gt;<br />     *<br />     * The implementor must also ensure that the relation is transitive:<br />     * &lt;tt&gt;((compare(x, y)&amp;gt;0) &amp;amp;&amp;amp; (compare(y, z)&amp;gt;0))&lt;/tt&gt; implies<br />     * &lt;tt&gt;compare(x, z)&amp;gt;0&lt;/tt&gt;.&lt;p&gt;<br />     *<br />     * Finally, the implementor must ensure that &lt;tt&gt;compare(x, y)==0&lt;/tt&gt;<br />     * implies that &lt;tt&gt;sgn(compare(x, z))==sgn(compare(y, z))&lt;/tt&gt; for all<br />     * &lt;tt&gt;z&lt;/tt&gt;.&lt;p&gt;<br />     *<br />     * It is generally the case, but &lt;i&gt;not&lt;/i&gt; strictly required that<br />     * &lt;tt&gt;(compare(x, y)==0) == (x.equals(y))&lt;/tt&gt;.  Generally speaking,<br />     * any comparator that violates this condition should clearly indicate<br />     * this fact.  The recommended language is "Note: this comparator<br />     * imposes orderings that are inconsistent with equals."<br />     *<br />     * </span><span style="color: rgb(128, 128, 128);">@param</span><span style="color: rgb(0, 128, 0);"> o1 the first object to be compared.<br />     * </span><span style="color: rgb(128, 128, 128);">@param</span><span style="color: rgb(0, 128, 0);"> o2 the second object to be compared.<br />     * </span><span style="color: rgb(128, 128, 128);">@return</span><span style="color: rgb(0, 128, 0);"> a negative integer, zero, or a positive integer as the<br />     *            first argument is less than, equal to, or greater than the<br />     *           second.<br />     * </span><span style="color: rgb(128, 128, 128);">@throws</span><span style="color: rgb(0, 128, 0);"> ClassCastException if the arguments' types prevent them from<br />     *            being compared by this comparator.<br />     </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> compare(T o1, T o2);<br /><br />    </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />     *<br />     * Indicates whether some other object is &amp;quot;equal to&amp;quot; this<br />     * comparator.  This method must obey the general contract of<br />     * {</span><span style="color: rgb(128, 128, 128);">@link</span><span style="color: rgb(0, 128, 0);"> Object#equals(Object)}.  Additionally, this method can return<br />     * &lt;tt&gt;true&lt;/tt&gt; &lt;i&gt;only&lt;/i&gt; if the specified object is also a comparator<br />     * and it imposes the same ordering as this comparator.  Thus,<br />     * &lt;code&gt;comp1.equals(comp2)&lt;/code&gt; implies that &lt;tt&gt;sgn(comp1.compare(o1,<br />     * o2))==sgn(comp2.compare(o1, o2))&lt;/tt&gt; for every object reference<br />     * &lt;tt&gt;o1&lt;/tt&gt; and &lt;tt&gt;o2&lt;/tt&gt;.&lt;p&gt;<br />     *<br />     * Note that it is &lt;i&gt;always&lt;/i&gt; safe &lt;i&gt;not&lt;/i&gt; to override<br />     * &lt;tt&gt;Object.equals(Object)&lt;/tt&gt;.  However, overriding this method may,<br />     * in some cases, improve performance by allowing programs to determine<br />     * that two distinct comparators impose the same order.<br />     *<br />     * </span><span style="color: rgb(128, 128, 128);">@param</span><span style="color: rgb(0, 128, 0);">   obj   the reference object with which to compare.<br />     * </span><span style="color: rgb(128, 128, 128);">@return</span><span style="color: rgb(0, 128, 0);">  &lt;code&gt;true&lt;/code&gt; only if the specified object is also<br />     *        a comparator and it imposes the same ordering as this<br />     *        comparator.<br />     * </span><span style="color: rgb(128, 128, 128);">@see</span><span style="color: rgb(0, 128, 0);"> Object#equals(Object)<br />     * </span><span style="color: rgb(128, 128, 128);">@see</span><span style="color: rgb(0, 128, 0);"> Object#hashCode()<br />     </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> equals(Object obj);<br />}<br /></span></span></div>我敢开始看觉得奇怪，好像在匿名内部类中没有实现方法boolean equals(Object obj);后来仔细一想，其实所有的类都是Object的子类，而Object正好有该方法的实现，所以，即使没有实现该方法也是合法的。<br /><img src ="http://www.blogjava.net/JafeLee/aggbug/119852.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-05-26 00:16 <a href="http://www.blogjava.net/JafeLee/archive/2007/05/26/119852.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java 移位操作</title><link>http://www.blogjava.net/JafeLee/archive/2007/05/22/119177.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Tue, 22 May 2007 09:17:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/05/22/119177.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/119177.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/05/22/119177.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/119177.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/119177.html</trackback:ping><description><![CDATA[移位操作要注意的问题是高（低）位是补0还是补1和对char, byte, short型的操作：<br />（1）&lt;&lt; : (left-shift), 最低位补0<br />（2）&gt;&gt; : （signed right-shift), 右移过程使用符号位扩展（sign extension)，即如果符号为为1则高位补1，  是0则补0，也就是逻辑右移<br />（3）&gt;&gt;&gt; : (unsigned right-shit),右移过程使用零扩展（zero extension),即最高位一律补0，也就是算术右移<br />（4）
移位操作的数据类型可以是byte, char, short, int, long型，但是对byte, char,
short进行操作时会先把它们变成一个int型，最后得到一个int型的结果，对long型操作时得到一个long型结果，不可以对boolean型进
行操作。<br />（5）移位操作符可以和＝合并起来，即 &lt;&lt;= 、 &gt;&gt;= 和 &gt;&gt;&gt;=。例如 a
&gt;&gt;= 2; 表示将a右移两位后的值重新赋给a。当时在使用这三个操作符对 byte, char,
short型数据进行操作时要注意，例如有一下代码片段：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> ShiftTest<br />{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String [] args)<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);"> a;<br />        </span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);"> b;<br />        </span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);"> c;<br />        a </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">127</span><span style="color: rgb(0, 0, 0);">;<br />        b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">127</span><span style="color: rgb(0, 0, 0);">;<br />        c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">127</span><span style="color: rgb(0, 0, 0);">;<br />        a </span><span style="color: rgb(0, 0, 0);">&lt;&lt;=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">;<br />        System.out.println(a);<br />        System.out.println(b </span><span style="color: rgb(0, 0, 0);">&lt;&lt;=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">);<br />        System.out.println(c </span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">);<br />    }<br />}<br />    </span></div>运行结果是：<br />            -4<br />              -4<br />                508<br />这
说明了在操作a &lt;&lt;= 2 执行过程是这样的：先将 byte型的数 127变成int型，左移2位得到
508，然后把508赋给byte型变量a时只是简单地"折断"(truncate)得到数-4。编译时编译器不会提示你可能损失精度（实际上在本例中确
实是损失精度了），但是如果你把a &lt;&lt;= 2改成 a = a &lt;&lt; 2;编译器就会提示可能损失精度了。<img src ="http://www.blogjava.net/JafeLee/aggbug/119177.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-05-22 17:17 <a href="http://www.blogjava.net/JafeLee/archive/2007/05/22/119177.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java 点滴 (1)</title><link>http://www.blogjava.net/JafeLee/archive/2007/05/21/118918.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Mon, 21 May 2007 08:30:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/05/21/118918.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/118918.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/05/21/118918.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/118918.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/118918.html</trackback:ping><description><![CDATA[
		<div align="left">
				<font color="#ff0000">1、</font>Java运算符优先级：<br /></div>
		<br />
		<table align="center" cellpadding="5" cellspacing="0" frame="hsides" rules="none">
				<caption>
						<h5 class="docTableTitle">Operator Precedence</h5>
				</caption>
				<colgroup>
						<col width="418" />
						<col width="132" />
				</colgroup>
				<thead>
						<tr>
								<th class="thead" scope="col" align="left" valign="top">
										<p class="docText">
												<span class="docEmphStrong">Operators</span>
										</p>
								</th>
								<th class="thead" scope="col" align="left" valign="top">
										<p class="docText">
												<span class="docEmphStrong">Associativity</span>
										</p>
								</th>
						</tr>
				</thead>
				<tbody>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>[] . ()</tt> (method call) <a name="ch03index171"></a><a name="ch03index172"></a><a name="ch03index173"></a><a name="ch03index174"></a><a name="ch03index175"></a><a name="ch03index176"></a><a name="ch03index177"></a></p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">Left to right</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>! ~ ++ -- +</tt> (unary) <tt>–</tt> (unary) <tt>()</tt> 
(cast) <tt>new</tt></p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">Right to left</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>* / %</tt>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">Left to right</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>+ -</tt>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">Left to right</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>&lt;&lt; &gt;&gt; &gt;&gt;&gt;</tt>
												<a name="ch03index178">
												</a>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">Left to right</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>&lt; &lt;= &gt; &gt;= instanceof</tt>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">Left to right</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>== !=</tt>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">Left to right</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>&amp;</tt>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">Left to right</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>^</tt>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">Left to right</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>|</tt>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">Left to right</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>&amp;&amp;</tt>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">Left to right</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>||</tt>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">Left to right</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>?:</tt>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">Right to left</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>= += -= *= /= %= &amp;= |= ^= &lt;&lt;= &gt;&gt;= 
&gt;&gt;&gt;=</tt>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">Right to left</p>
								</td>
						</tr>
				</tbody>
		</table>
		<font color="#ff0000">
				<br />
				注意：<br /></font>
		<ul>
				<li>&amp;&amp; || ! 的操作数只能用于boolean 或 Boolean</li>
				<li>&amp;  | 的操作数既可以用于boolean(Boolean)或者整形(不仅仅是int型），但两个操作数的类型必须一致</li>
				<li>~不能用于boolean(Boolean)型，但可以用于整形，大概是为了和 ! 区别开来吧</li>
				<li>&amp; | 没有短路计算的<br /></li>
		</ul>
		<font color="#ff0000">2、</font>
		<table align="center" cellpadding="5" cellspacing="0" frame="hsides" height="236" rules="none" width="747">
				<caption>
						<h5 class="docTableTitle">Java Integer Types</h5>
				</caption>
				<colgroup>
						<col width="115" />
						<col width="181" />
						<col width="253" />
				</colgroup>
				<thead>
						<tr>
								<th class="thead" scope="col" align="left" valign="top">
										<p class="docText">
												<span class="docEmphStrong">Type</span>
										</p>
								</th>
								<th class="thead" scope="col" align="left" valign="top">
										<p class="docText">
												<span class="docEmphStrong">Storage Requirement</span>
										</p>
								</th>
								<th class="thead" scope="col" align="left" valign="top">
										<p class="docText">
												<span class="docEmphStrong">Range 
(Inclusive)</span>
										</p>
								</th>
						</tr>
				</thead>
				<tbody>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>int</tt>
												<a name="ch03index21">
												</a>
												<a name="ch03index22">
												</a>
												<a name="ch03index23">
												</a>
												<a name="ch03index24">
												</a>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">4 bytes</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">–2,147,483,648 to 2,147,483,647 (just over 2 billion)</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>short</tt>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">2 bytes</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">–32,768 to 32,767</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>long</tt>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">8 bytes</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">–9,223,372,036,854,775,808 to 
9,223,372,036,854,775,807</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>byte</tt>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">1 byte</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">–128 to 127</p>
								</td>
						</tr>
				</tbody>
		</table>
		<div align="left">     
				<font color="#ff0000"><font color="#000000">char                             2 bytes                                      0 to 65535</font><br /><br />3、</font></div>
		<p align="center">
		</p>
		<table align="center" cellpadding="5" cellspacing="0" frame="hsides" height="181" rules="none" width="747">
				<caption>
						<h5 class="docTableTitle">Floating-Point Types</h5>
				</caption>
				<colgroup>
						<col width="88" />
						<col width="170" />
						<col width="291" />
				</colgroup>
				<thead>
						<tr>
								<th class="thead" scope="col" align="left" valign="top">
										<p class="docText">
												<span class="docEmphStrong">Type</span>
										</p>
								</th>
								<th class="thead" scope="col" align="left" valign="top">
										<p class="docText">
												<span class="docEmphStrong">Storage Requirement</span>
										</p>
								</th>
								<th class="thead" scope="col" align="left" valign="top">
										<p class="docText">
												<span class="docEmphStrong">Range</span>
										</p>
								</th>
						</tr>
				</thead>
				<tbody>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>float</tt>
												<a name="ch03index34">
												</a>
												<a name="ch03index35">
												</a>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">4 bytes</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">approximately ±3.40282347E+38F (6–7 significant decimal 
digits)</p>
								</td>
						</tr>
						<tr>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">
												<tt>double</tt>
										</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">8 bytes</p>
								</td>
								<td class="docTableCell" align="left" valign="top">
										<p class="docText">approximately ±1.79769313486231570E+308 (15 significant decimal 
digits)</p>
								</td>
						</tr>
				</tbody>
		</table>
		<br />4、final 实例域（final instance field)：可以将实例域定义为final，构建对象时必须初始化这样得域，也就是说，必须确保在每一个构造器执行之后，这个域的值被设置。但是，静态常量必须在声明的同时也被初始化。<br /><br />5、for each 循环：<br />for (variable : collection) statment<br />例如：for (int element : a)<br />            System.out.println(element);<br />该代码片段打印数组a的每一个元素，一个元素占一行<br /><br />6、合法的Java标识符：可以以下划线、字母或美元符号$开头，后面可以跟下划线、数字、字母、美元符号。1.42以后的java标识符也可以包含汉字，包括开头。<br /><img src ="http://www.blogjava.net/JafeLee/aggbug/118918.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-05-21 16:30 <a href="http://www.blogjava.net/JafeLee/archive/2007/05/21/118918.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java abstract 类</title><link>http://www.blogjava.net/JafeLee/archive/2007/05/20/118706.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Sun, 20 May 2007 10:27:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/05/20/118706.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/118706.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/05/20/118706.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/118706.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/118706.html</trackback:ping><description><![CDATA[    <font size="3">虽然接触Java已经快一年了，也系统的学过Java语法，不过很多概念到现在都搞不清楚，现在决定重新把这些模糊的概念重新温习一遍，今天就现总结一下抽象类（abstract class)。<br /><br />
1、使用abstract类的动机：可以参考 <span class="v1">Cay S. Horstmann</span>, <span class="v1">Gary Cornel 写的书《<strong>Core Java™ 2 Volume I - Fundamentals, Seventh Edition</strong>
》（顺便罗嗦一下，该书是偶见过最好的Java入门书） “Classes, Superclasses, and Subclasses
”一节，定义一个abstract class的格式是：修饰符（public, private, etc)+abstract+类名 <font color="#ff0000">或者</font> abstract + 修饰符 + 类名。</span><br /><span class="v1"></span><br /><span class="v1">2、如果一个类至少存在一个抽象方法(abstract method)，则它自身必须声明成一个抽象类。但一个类可以被声明成一个抽象类即使它没有任何抽象方法。声明一个abstrac</span>t method的格式是：</font><font size="3"><span class="v1">修饰符（public, private, etc)+abstract+返回类型 + 方法名 <font color="#ff0000">或者</font> abstract + 修饰符 + 返回类型 + 方法名。</span></font><br /><font size="3"><span class="v1"></span><br /><span class="v1">3、一个抽象类可以有数据域和非抽象方法（concrete data and concrete methods)。</span><br /><span class="v1"></span><br /><span class="v1">4、当一个子类继承某个抽象类时，它可以有两个选择：<br />    (1)    部分实现或完全不实现父类的所有抽象方法，但此时子类必须声明为抽象类。<br />    (2)    实现父类所有的抽象方法，此时之类不比声明为抽象类。<br /><br />5、抽象类不能被实例化（be instantiated)，但可以实例化非抽象子类（concrete subclass)。<br />   可以声明抽象类变量，但该变量必须指向一个非抽象子类</span><span class="v1">.看下面一个例子：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">.  Person [] people </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Person[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">];    <br />   </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">. people[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Employee(<img src="http://www.blogjava.net/images/dot.gif" />);    <br />   </span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">. people[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Student(<img src="http://www.blogjava.net/images/dot.gif" />);  <br />   </span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">. </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> (Person p : people)  <br />   </span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">.    System.out.println(p.getName() </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> p.getDescription());  <br /></span></div></span></font><span class="v1"><font size="3"><font size="3">注意代码中的一个调用:p.getDescription().可能有人会担心这里调用了一个没定义的方法。但是， 由于<font size="3">不能构造抽象类Person的对象，所以p永远不会调动Person对象，而是只会调用诸如Employee或Student这</font></font><font size="3">样的具体子类方法。</font></font><font color="#ff0000" size="3">注意，如果没有定义Person中抽象方法getDescription()但是通过p调用getDescription()则会产生编译错误，编译器只允许调用在类中声明的方法。</font></span><font size="3"><br /></font><span class="v1"><font size="3"><br />6、摘自《Core Java 2》的一个例子：</font><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_075958" onclick="this.style.display='none'; Code_Closed_Text_075958.style.display='none'; Code_Open_Image_075958.style.display='inline'; Code_Open_Text_075958.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_075958" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_075958.style.display='none'; Code_Closed_Image_075958.style.display='inline'; Code_Closed_Text_075958.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_075958" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">PersonTest.java</span><span id="Code_Open_Text_075958" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />   </span><span style="color: rgb(128, 128, 128);">@version</span><span style="color: rgb(0, 128, 0);"> 1.01 2004-02-21<br />   </span><span style="color: rgb(128, 128, 128);">@author</span><span style="color: rgb(0, 128, 0);"> Cay Horstmann<br /></span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.text.</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> PersonTest<br />{  <br />   </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args)<br />   {  <br />      Person[] people </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Person[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">];<br /><br />      </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> fill the people array with Student and Employee objects</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">      people[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Employee(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Harry Hacker</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">50000</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">1989</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br />      people[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Student(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Maria Morris</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">computer science</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />      </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> print out names and descriptions of all Person objects</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">      </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> (Person p : people)<br />         System.out.println(p.getName() </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> p.getDescription());<br />   }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Person<br />{  <br />   </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Person(String n)<br />   {  <br />      name </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> n;<br />   }<br /><br />   </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);"> String getDescription();<br /><br />   </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String getName()<br />   {  <br />      </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> name;<br />   }<br /><br />   </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> String name;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Employee </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Person<br />{  <br />   </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Employee(String n, </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> s,<br />      </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> year, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> month, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> day)<br />   {  <br />      </span><span style="color: rgb(0, 0, 255);">super</span><span style="color: rgb(0, 0, 0);">(n);<br />      salary </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> s;<br />      GregorianCalendar calendar </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> GregorianCalendar(year, month </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, day);<br />      hireDay </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> calendar.getTime();<br />   }<br /><br />   </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> getSalary()<br />   {  <br />      </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> salary;<br />   }<br /><br />   </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Date getHireDay()<br />   {  <br />      </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> hireDay;<br />   }<br /><br />   </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String getDescription()<br />   {  <br />      </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> String.format(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">an employee with a salary of $%.2f</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, salary);<br />   }<br /><br />   </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> raiseSalary(</span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> byPercent)<br />   {  <br />      </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> raise </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> salary </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> byPercent </span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">;<br />      salary </span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);"> raise;<br />   }<br /><br />   </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> salary;<br />   </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> Date hireDay;<br />}<br /><br /><br /></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Student </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Person<br />{  <br />   </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />      </span><span style="color: rgb(128, 128, 128);">@param</span><span style="color: rgb(0, 128, 0);"> n the student's name<br />      </span><span style="color: rgb(128, 128, 128);">@param</span><span style="color: rgb(0, 128, 0);"> m the student's major<br />   </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />   </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Student(String n, String m)<br />   {  <br />      </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> pass n to superclass constructor</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">      </span><span style="color: rgb(0, 0, 255);">super</span><span style="color: rgb(0, 0, 0);">(n);<br />      major </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> m;<br />   }<br /><br />   </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> String getDescription()<br />   {  <br />      </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">a student majoring in </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> major;<br />   }<br /><br />   </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> String major;<br />}<br /></span></span></div><br />参考资料：<br /><br /></span><p style="color: rgb(96, 24, 255);"><span class="v1"><span style="font-weight: bold; font-size: 18pt;">Core Java 2 Volume I - Fundamentals, Seventh Edition</span></span></p><p>by Cay S. Horstman, Gary Cornell<br /></p><span class="v1"><br /></span><span class="v1"></span><span class="v1"></span><br /><span class="v1"></span><img src ="http://www.blogjava.net/JafeLee/aggbug/118706.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-05-20 18:27 <a href="http://www.blogjava.net/JafeLee/archive/2007/05/20/118706.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>