﻿<?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-忆风-随笔分类-Java SE</title><link>http://www.blogjava.net/yifeng/category/34012.html</link><description>光是知道是不够的，必须要加以应用；光是希望是不够的，非去做不可。</description><language>zh-cn</language><lastBuildDate>Wed, 17 Dec 2008 21:18:38 GMT</lastBuildDate><pubDate>Wed, 17 Dec 2008 21:18:38 GMT</pubDate><ttl>60</ttl><item><title>Java keywords</title><link>http://www.blogjava.net/yifeng/archive/2008/12/17/246751.html</link><dc:creator>忆风</dc:creator><author>忆风</author><pubDate>Tue, 16 Dec 2008 16:09:00 GMT</pubDate><guid>http://www.blogjava.net/yifeng/archive/2008/12/17/246751.html</guid><wfw:comment>http://www.blogjava.net/yifeng/comments/246751.html</wfw:comment><comments>http://www.blogjava.net/yifeng/archive/2008/12/17/246751.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/yifeng/comments/commentRss/246751.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yifeng/services/trackbacks/246751.html</trackback:ping><description><![CDATA[<p>In the <a title="Java (programming language)" href="http://en.wikipedia.org/wiki/Java_(programming_language)">Java programming language</a>, a <strong>keyword</strong> is one of 50 <a title="Reserved word" href="http://en.wikipedia.org/wiki/Reserved_word">reserved words</a> which have a predefined meaning in the language; because of this, programmers cannot use keywords as names for <a title="Variable" href="http://en.wikipedia.org/wiki/Variable#Computer_programming">variables</a>, <a title="Method (computer science)" href="http://en.wikipedia.org/wiki/Method_(computer_science)">methods</a>, <a title="Class (computer science)" href="http://en.wikipedia.org/wiki/Class_(computer_science)">classes</a>, or as any other <a title="Identifier" href="http://en.wikipedia.org/wiki/Identifier">identifier</a>.<sup class="reference" id="cite_ref-keywords_0-0"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-keywords-0"><span>[</span>1<span>]</span></a></sup> Due to their special functions in the language, most <a title="Integrated development environment" href="http://en.wikipedia.org/wiki/Integrated_development_environment">integrated development environments</a> for Java use <a title="Syntax highlighting" href="http://en.wikipedia.org/wiki/Syntax_highlighting">syntax highlighting</a> to display keywords in a different color for easy identification.<sup class="noprint Template-Fact"><span title="This claim needs references to reliable sources&nbsp;since December 2008" style="white-space: nowrap">[<em><a title="Wikipedia:Citation needed" href="http://en.wikipedia.org/wiki/Wikipedia:Citation_needed">citation needed</a></em>]</span></sup></p>
<p>The following is a list of the keywords in Java, along with brief descriptions of their functions:<sup class="reference" id="cite_ref-keywords_0-1"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-keywords-0"><span>[</span>1<span>]</span></a></sup></p>
<dl>
<dt><code><a class="mw-redirect" title="Abstract class" href="http://en.wikipedia.org/wiki/Abstract_class">abstract</a></code>
<dd>The <code>abstract</code> keyword is used to declare a class or method to be <a title="Abstract type" href="http://en.wikipedia.org/wiki/Abstract_type">abstract</a>. An abstract method has no implementation; all classes containing abstract methods must themselves be abstract, although not all abstract classes have abstract methods. <a title="Object (computer science)" href="http://en.wikipedia.org/wiki/Object_(computer_science)">Objects</a> of a class which is abstract cannot be <a title="Object (computer science)" href="http://en.wikipedia.org/wiki/Object_(computer_science)">instantiated</a>, but can be <a title="Inheritance (computer science)" href="http://en.wikipedia.org/wiki/Inheritance_(computer_science)">extended</a> by other classes. All <a title="Subclass (computer science)" href="http://en.wikipedia.org/wiki/Subclass_(computer_science)">subclasses</a> of an abstract class must either provide implementations for all abstract methods, or must also be abstract.<sup class="reference" id="cite_ref-abstract_1-0"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-abstract-1"><span>[</span>2<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code><a title="Assertion (computing)" href="http://en.wikipedia.org/wiki/Assertion_(computing)">assert</a></code>
<dd>The <code>assert</code> keyword, which was added in <a title="Java version history" href="http://en.wikipedia.org/wiki/Java_version_history#J2SE_1.4_.28February_6.2C_2002.29_.28Unsupported.29">J2SE 1.4</a>,<sup class="reference" id="cite_ref-keywords_0-2"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-keywords-0"><span>[</span>1<span>]</span></a></sup> is used to make an <a title="Assertion (computing)" href="http://en.wikipedia.org/wiki/Assertion_(computing)">assertion</a>—a statement which the programmer believes is always true at that point in the program. If assertions are enabled when the program is run and it turns out that an assertion is false, an <code><a class="external text" title="http://java.sun.com/javase/6/docs/api/java/lang/AssertionError.html" href="http://java.sun.com/javase/6/docs/api/java/lang/AssertionError.html" rel="nofollow">AssertionError</a></code> is thrown and the program terminates. This keyword is inteded to aid in <a title="Debugging" href="http://en.wikipedia.org/wiki/Debugging">debugging</a>.<sup class="reference" id="cite_ref-assert_2-0"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-assert-2"><span>[</span>3<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code><a title="Boolean datatype" href="http://en.wikipedia.org/wiki/Boolean_datatype#Java">boolean</a></code>
<dd>The <code>boolean</code> keyword is used to declare a <a title="Field (computer science)" href="http://en.wikipedia.org/wiki/Field_(computer_science)">field</a> that can store a <a title="Boolean datatype" href="http://en.wikipedia.org/wiki/Boolean_datatype">boolean value</a>; that is, either <code>true</code> or <code>false</code>.<sup class="reference" id="cite_ref-primitive_3-0"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-primitive-3"><span>[</span>4<span>]</span></a></sup> This keyword is also used to declare that a method returns a value of type <code>boolean</code>.<sup class="reference" id="cite_ref-return_4-0"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-return-4"><span>[</span>5<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code><a title="Switch statement" href="http://en.wikipedia.org/wiki/Switch_statement#Java">break</a></code>
<dd>Used to resume program execution at the statement immediately following the current enclosing block or statement. If followed by a <a title="Label (programming language)" href="http://en.wikipedia.org/wiki/Label_(programming_language)">label</a>, the program resumes execution at the statement immediately following the enclosing labeled statement or block. </dd></dl>
<dl>
<dt><code><a title="Byte" href="http://en.wikipedia.org/wiki/Byte">byte</a></code>
<dd>The <code>byte</code> keyword is used to declare a field that can store an 8-<a title="Bit" href="http://en.wikipedia.org/wiki/Bit">bit</a> signed <a title="Two's complement" href="http://en.wikipedia.org/wiki/Two%27s_complement">two's complement</a> integer.<sup class="reference" id="cite_ref-primitive_3-1"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-primitive-3"><span>[</span>4<span>]</span></a></sup> This keyword is also used to declare that a method returns a value of type <code>byte</code>.<sup class="reference" id="cite_ref-return_4-1"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-return-4"><span>[</span>5<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code id="case"><a title="Switch statement" href="http://en.wikipedia.org/wiki/Switch_statement#Java">case</a></code>
<dd>The <code>case</code> keyword is used to create individual cases in a <a title="Switch statement" href="http://en.wikipedia.org/wiki/Switch_statement">switch statement</a>; see <em><code><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#switch">switch</a></code></em>.<sup class="reference" id="cite_ref-switch_5-0"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-switch-5"><span>[</span>6<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code><a title="Exception handling syntax" href="http://en.wikipedia.org/wiki/Exception_handling_syntax#Java">catch</a></code>
<dd>Defines an <a class="mw-redirect" title="Exception handler" href="http://en.wikipedia.org/wiki/Exception_handler">exception handler</a>—a group of statements that are executed if an exception is thrown in the block defined by a preceding <code>try</code> keyword. The code is executed only if the class of the thrown exception is assignment compatible with the exception class declared by the <code>catch</code> clause. </dd></dl>
<dl>
<dt><code><a title="Character (computing)" href="http://en.wikipedia.org/wiki/Character_(computing)">char</a></code>
<dd>The <code>char</code> keyword is used to declare a field that can store a 16-bit <a title="Unicode" href="http://en.wikipedia.org/wiki/Unicode">Unicode</a> character.<sup class="reference" id="cite_ref-primitive_3-2"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-primitive-3"><span>[</span>4<span>]</span></a></sup> This keyword is also used to declare that a method returns a value of type <code>char</code>.<sup class="reference" id="cite_ref-return_4-2"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-return-4"><span>[</span>5<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code><a title="Class (computer science)" href="http://en.wikipedia.org/wiki/Class_(computer_science)#Java">class</a></code>
<dd>A type that defines the implementation of a particular kind of object. A class definition defines <a title="Object (computer science)" href="http://en.wikipedia.org/wiki/Object_(computer_science)">instance</a> and class fields, <a title="Method (computer science)" href="http://en.wikipedia.org/wiki/Method_(computer_science)">methods</a>, and <a title="Inner class" href="http://en.wikipedia.org/wiki/Inner_class">inner classes</a> as well as specifying the <a title="Interface (computer science)" href="http://en.wikipedia.org/wiki/Interface_(computer_science)">interfaces</a> the class implements and the immediate <a title="Superclass (computer science)" href="http://en.wikipedia.org/wiki/Superclass_(computer_science)">superclass</a> of the class. If the superclass is not explicitly specified, the superclass is implicitly <code><a class="external text" title="http://java.sun.com/javase/6/docs/api/java/lang/Object.html" href="http://java.sun.com/javase/6/docs/api/java/lang/Object.html" rel="nofollow">Object</a></code>. </dd></dl>
<dl>
<dt><code><a title="Variable" href="http://en.wikipedia.org/wiki/Variable#Constant">const</a></code>
<dd>Although reserved as a keyword in Java, <code>const</code> is not used and has no function.<sup class="reference" id="cite_ref-keywords_0-3"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-keywords-0"><span>[</span>1<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code><a class="mw-redirect" title="Continue (Java)" href="http://en.wikipedia.org/wiki/Continue_(Java)">continue</a></code>
<dd>Used to resume program execution at the end of the current loop body. If followed by a label, <code>continue</code> resumes execution at the end of the enclosing labeled loop body. </dd></dl>
<dl>
<dt><code id="default"><a title="Switch statement" href="http://en.wikipedia.org/wiki/Switch_statement#Java">default</a></code>
<dd>The <code>default</code> can optionally be used in a <a title="Switch statement" href="http://en.wikipedia.org/wiki/Switch_statement">switch statement</a> to label a block of statements to be executed if no <code>case</code> matches the specified value; see <em><code><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#switch">switch</a></code></em>.<sup class="reference" id="cite_ref-switch_5-1"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-switch-5"><span>[</span>6<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code id="do"><a title="Do while loop" href="http://en.wikipedia.org/wiki/Do_while_loop#Java">do</a></code>
<dd>The <code>do</code> keyword is used in conjunction with <code><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#while">while</a></code> to create a <a class="mw-redirect" title="Do-while loop" href="http://en.wikipedia.org/wiki/Do-while_loop">do-while loop</a>, which executes a block of statements assocated with the loop and then tests a boolean expression assocated with the <code>while</code>. If the expression evaluates to <code>true</code>, the block is executed again; this continues until the expression evaluates to <code>false</code>.<sup class="reference" id="cite_ref-do-while_6-0"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-do-while-6"><span>[</span>7<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code><a title="Double precision" href="http://en.wikipedia.org/wiki/Double_precision">double</a></code>
<dd>The <code>double</code> keyword is used to declare a field that can hold a 64-bit <a title="Double precision" href="http://en.wikipedia.org/wiki/Double_precision">double precision</a> <a class="mw-redirect" title="IEEE 754" href="http://en.wikipedia.org/wiki/IEEE_754">IEEE 754</a> <a class="mw-redirect" title="Floating-point number" href="http://en.wikipedia.org/wiki/Floating-point_number">floating-point number</a>.<sup class="reference" id="cite_ref-primitive_3-3"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-primitive-3"><span>[</span>4<span>]</span></a></sup> This keyword is also used to declare that a method returns a value of type <code>double</code>.<sup class="reference" id="cite_ref-return_4-3"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-return-4"><span>[</span>5<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code id="else"><a class="mw-redirect" title="Conditional statement" href="http://en.wikipedia.org/wiki/Conditional_statement">else</a></code>
<dd>The <code>else</code> keyword is used in conjunction with <code><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#if">if</a></code> to create an <a title="Conditional (programming)" href="http://en.wikipedia.org/wiki/Conditional_(programming)">if-else statement</a>, which tests a <a title="Boolean expression" href="http://en.wikipedia.org/wiki/Boolean_expression">boolean expression</a>; if the expression evaluates to <code>true</code>, the block of statements associated with the <code>if</code> are evaluated; if it evaluates to <code>false</code>, the block of statements associated with the <code>else</code> are evaluated.<sup class="reference" id="cite_ref-if-else_7-0"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-if-else-7"><span>[</span>8<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code><a title="Enumerated type" href="http://en.wikipedia.org/wiki/Enumerated_type">enum</a></code> (as of J2SE 5.0)
<dd>A Java keyword used to declare an <a title="Enumerated type" href="http://en.wikipedia.org/wiki/Enumerated_type">enumerated type</a>. Enumerations extend the base class <code><a class="external text" title="http://java.sun.com/javase/6/docs/api/java/lang/Enum.html" href="http://java.sun.com/javase/6/docs/api/java/lang/Enum.html" rel="nofollow">Enum</a></code>. </dd></dl>
<dl>
<dt><code><a class="mw-redirect" title="Inheritance (object-oriented programming)" href="http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Java">extends</a></code>
<dd>Used in a class declaration to specify the superclass; used in an interface declaration to specify one or more superinterfaces. Class X extends class Y to add functionality, either by adding fields or methods to class Y, or by overriding methods of class Y. An interface Z extends one or more interfaces by adding methods. Class X is said to be a subclass of class Y; Interface Z is said to be a subinterface of the interfaces it extends.
<dd>Also used to specify an upper bound on a type parameter in Generics. </dd></dl>
<dl>
<dt><code><a title="Final (Java)" href="http://en.wikipedia.org/wiki/Final_(Java)">final</a></code>
<dd>Define an entity once that cannot be changed nor derived from later. More specifically: a final class cannot be subclassed, a final method cannot be overridden, and a final variable can occur at most once as a left-hand expression. All methods in a final class are implicitly <code>final</code>. </dd></dl>
<dl>
<dt><code><a title="Exception handling syntax" href="http://en.wikipedia.org/wiki/Exception_handling_syntax#Java">finally</a></code>
<dd>Used to define a block of statements for a block defined previously by the <code>try</code> keyword. The <code>finally</code> block is executed after execution exits the <code>try</code> block and any associated <code>catch</code> clauses regardless of whether an exception was thrown or caught, or execution left method in the middle of the <code>try</code> or <code>catch</code> blocks using the <code>return</code> keyword. </dd></dl>
<dl>
<dt><code><a title="Single precision" href="http://en.wikipedia.org/wiki/Single_precision">float</a></code>
<dd>The <code>float</code> keyword is used to declare a field that can hold a 32-bit <a title="Single precision" href="http://en.wikipedia.org/wiki/Single_precision">single precision</a> IEEE 754 floating-point number.<sup class="reference" id="cite_ref-primitive_3-4"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-primitive-3"><span>[</span>4<span>]</span></a></sup> This keyword is also used to declare that a method returns a value of type <code>float</code>.<sup class="reference" id="cite_ref-return_4-4"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-return-4"><span>[</span>5<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code><a title="For loop" href="http://en.wikipedia.org/wiki/For_loop#Java">for</a></code>
<dd>The <code>for</code> keyword is used to create a <a title="For loop" href="http://en.wikipedia.org/wiki/For_loop">for loop</a>, which specifies a variable initialization, a <a title="Boolean expression" href="http://en.wikipedia.org/wiki/Boolean_expression">boolean expression</a>, and an incrementation. The variable initialization is performed first, and then the boolean expression is evaluated. If the expression evaluates to <code>true</code>, the block of statements assocated with the loop are executed, and then the incrementation is performed. The boolean expression is then evaluated again; this continues until the expression evaluates to <code>false</code>.<sup class="reference" id="cite_ref-for_8-0"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-for-8"><span>[</span>9<span>]</span></a></sup> </dd></dl>
<dl>
<dd>As of <a title="Java version history" href="http://en.wikipedia.org/wiki/Java_version_history#J2SE_5.0_.28September_30.2C_2004.29_.28EOL.29">J2SE 5.0</a>,<sup class="noprint Template-Fact"><span title="This claim needs references to reliable sources&nbsp;since December 2008" style="white-space: nowrap">[<em><a title="Wikipedia:Citation needed" href="http://en.wikipedia.org/wiki/Wikipedia:Citation_needed">citation needed</a></em>]</span></sup> the <code>for</code> keyword can also be used to create a so-called "<a class="mw-redirect" title="For each loop" href="http://en.wikipedia.org/wiki/For_each_loop">enhanced for loop</a>", which specifies an <a title="Array" href="http://en.wikipedia.org/wiki/Array">array</a> or <code><a class="external text" title="http://java.sun.com/javase/6/docs/api/java/lang/Iterable.html" href="http://java.sun.com/javase/6/docs/api/java/lang/Iterable.html" rel="nofollow">Iterable</a></code> object; each iteration of the loop executes the associated block of statements using a different element in the array or <code>Iterable</code>.<sup class="reference" id="cite_ref-for_8-1"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-for-8"><span>[</span>9<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code><a class="mw-redirect" title="GOTO" href="http://en.wikipedia.org/wiki/GOTO">goto</a></code>
<dd>Although reserved as a keyword in Java, <code>goto</code> is not used and has no function.<sup class="reference" id="cite_ref-keywords_0-4"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-keywords-0"><span>[</span>1<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code id="if"><a class="mw-redirect" title="If statement" href="http://en.wikipedia.org/wiki/If_statement">if</a></code>
<dd>The <code>if</code> keyword is used to create an <a class="mw-redirect" title="If statement" href="http://en.wikipedia.org/wiki/If_statement">if statement</a>, which tests a <a title="Boolean expression" href="http://en.wikipedia.org/wiki/Boolean_expression">boolean expression</a>; if the expression evaluates to <code>true</code>, the block of statements associated with the if statement is executed. This keyword can also be used to create an <a title="Conditional (programming)" href="http://en.wikipedia.org/wiki/Conditional_(programming)">if-else statement</a>; see <em><code><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#else">else</a></code></em>.<sup class="reference" id="cite_ref-if-else_7-1"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-if-else-7"><span>[</span>8<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code>implements</code>
<dd>Included in a class declaration to specify one or more <a title="Interface (Java)" href="http://en.wikipedia.org/wiki/Interface_(Java)">interfaces</a> that are implemented by the current class. A class inherits the types and abstract methods declared by the interfaces. </dd></dl>
<dl>
<dt><code>import</code>
<dd>Used at the beginning of a <a class="mw-redirect" title="Source file" href="http://en.wikipedia.org/wiki/Source_file">source file</a> to specify classes or entire <a title="Java package" href="http://en.wikipedia.org/wiki/Java_package">Java packages</a> to be referred to later without including their package names in the reference. Since J2SE 5.0, <code>import</code> statements can import <code>static</code> members of a class. </dd></dl>
<dl>
<dt><code>instanceof</code>
<dd>A <a title="Operator (programming)" href="http://en.wikipedia.org/wiki/Operator_(programming)">binary operator</a> that takes an object reference as its first operand and a class or interface as its second operand and produces a boolean result. The <code>instanceof</code> operator evaluates to true if and only if the runtime type of the object is assignment compatible with the class or interface. </dd></dl>
<dl>
<dt><code><a title="Integer (computer science)" href="http://en.wikipedia.org/wiki/Integer_(computer_science)">int</a></code>
<dd>The <code>int</code> keyword is used to declare a field that can hold a 32-bit signed two's complement integer.<sup class="reference" id="cite_ref-primitive_3-5"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-primitive-3"><span>[</span>4<span>]</span></a></sup> This keyword is also used to declare that a method returns a value of type <code>int</code>.<sup class="reference" id="cite_ref-return_4-5"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-return-4"><span>[</span>5<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code><a title="Interface (Java)" href="http://en.wikipedia.org/wiki/Interface_(Java)">interface</a></code>
<dd>Used to declare a special type of class that only contains abstract methods, constant (<code>static final</code>) fields and <code>static</code> interfaces. It can later be implemented by classes that declare the interface with the <code>implements</code> keyword. </dd></dl>
<dl>
<dt><code><a title="Long integer" href="http://en.wikipedia.org/wiki/Long_integer">long</a></code>
<dd>The <code>long</code> keyword is used to declare a field that can hold a 64-bit signed two's complement integer.<sup class="reference" id="cite_ref-primitive_3-6"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-primitive-3"><span>[</span>4<span>]</span></a></sup> This keyword is also used to declare that a method returns a value of type <code>long</code>.<sup class="reference" id="cite_ref-return_4-6"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-return-4"><span>[</span>5<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code>native</code>
<dd>Used in method declarations to specify that the method is not implemented in the same Java source file, but rather in another language. </dd></dl>
<dl>
<dt><code><a title="Object lifetime" href="http://en.wikipedia.org/wiki/Object_lifetime#Java">new</a></code>
<dd>Used to create an instance of a class or array. </dd></dl>
<dl>
<dt><code><a title="Java package" href="http://en.wikipedia.org/wiki/Java_package">package</a></code>
<dd>A group of types. Packages are declared with the <code>package</code> keyword. </dd></dl>
<dl>
<dt><code><a title="Java syntax" href="http://en.wikipedia.org/wiki/Java_syntax#Class_member_access">private</a></code>
<dd>The <code>private</code> keyword is used in the declaration of a method, field, or inner class; private members can only be accessed by other members of their own class.<sup class="reference" id="cite_ref-access_9-0"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-access-9"><span>[</span>10<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code><a title="Java syntax" href="http://en.wikipedia.org/wiki/Java_syntax#Class_member_access">protected</a></code>
<dd>The <code>protected</code> keyword is used in the declaration of a method, field, or inner class; protected members can only be accessed by members of ther own class or that class's <a class="mw-redirect" title="Inheritance (object-oriented programming)" href="http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)">subclasses</a>.<sup class="reference" id="cite_ref-access_9-1"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-access-9"><span>[</span>10<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code><a title="Java syntax" href="http://en.wikipedia.org/wiki/Java_syntax#Class_member_access">public</a></code>
<dd>The <code>public</code> keyword is used in the declaration of a class, method, or field; public classes, methods, and fields can be accessed by the members of any class.<sup class="reference" id="cite_ref-access_9-2"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-access-9"><span>[</span>10<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code><a title="Method (computer science)" href="http://en.wikipedia.org/wiki/Method_(computer_science)">return</a></code>
<dd>Used to finish the execution of a method. It can be followed by a value required by the method definition that is returned to the caller. </dd></dl>
<dl>
<dt><code><a title="Short integer" href="http://en.wikipedia.org/wiki/Short_integer">short</a></code>
<dd>The <code>short</code> keyword is used to declare a field that can hold a 16-bit signed two's complement integer.<sup class="reference" id="cite_ref-primitive_3-7"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-primitive-3"><span>[</span>4<span>]</span></a></sup> This keyword is also used to declare that a method returns a value of type <code>short</code>.<sup class="reference" id="cite_ref-return_4-7"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-return-4"><span>[</span>5<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code><a title="Static variable" href="http://en.wikipedia.org/wiki/Static_variable">static</a></code>
<dd>Used to declare a field, method or inner class as a class field. Classes maintain one copy of class fields regardless of how many instances exist of that class. <code>static</code> also is used to define a method as a class method. Class methods are <a title="Name binding" href="http://en.wikipedia.org/wiki/Name_binding">bound</a> to the class instead of to a specific instance, and can only operate on class fields. (Classes and interfaces declared as <code>static</code> members of another class or interface are actually top-level classes and are <em>not</em> inner classes.) </dd></dl>
<dl>
<dt><code><a title="Strictfp" href="http://en.wikipedia.org/wiki/Strictfp">strictfp</a></code> (as of J2SE 1.2)
<dd>A Java keyword used to restrict the precision and rounding of floating point calculations to ensure portability. </dd></dl>
<dl>
<dt><code><a class="mw-redirect" title="Inheritance (object-oriented programming)" href="http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Java">super</a></code>
<dd>Used to access members of a class inherited by the class in which it appears. Allows a subclass to access <a title="Method overriding (programming)" href="http://en.wikipedia.org/wiki/Method_overriding_(programming)">overridden</a> methods and hidden members of its superclass. The <code>super</code> keyword is also used to forward a call from a constructor to a constructor in the superclass.
<dd>Also used to specify a lower bound on a type parameter in Generics. </dd></dl>
<dl>
<dt><code id="switch"><a title="Switch statement" href="http://en.wikipedia.org/wiki/Switch_statement#Java">switch</a></code>
<dd>The <code>switch</code> keyword is used in conjunction with <code><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#case">case</a></code> and <code><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#default">default</a></code> to create a <a title="Switch statement" href="http://en.wikipedia.org/wiki/Switch_statement">switch statement</a>, which evaluates a variable, matches its value to a specific <code>case</code>, and executes the block of statements associated with that <code>case</code>. If no <code>case</code> matches the value, the optional block labelled by <code>default</code> is executed, if included.<sup class="reference" id="cite_ref-switch_5-2"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-switch-5"><span>[</span>6<span>]</span></a></sup> </dd></dl>
<dl>
<dt><code><a title="Mutual exclusion" href="http://en.wikipedia.org/wiki/Mutual_exclusion">synchronized</a></code>
<dd>Used in the declaration of a method or code block to acquire the <a class="mw-redirect" title="Mutex" href="http://en.wikipedia.org/wiki/Mutex">mutex</a> lock for an object while the current <a title="Thread (computer science)" href="http://en.wikipedia.org/wiki/Thread_(computer_science)">thread</a> executes the code. For static methods, the object locked is the class' <code>Class</code>. Guarantees that at most one thread at a time operating on the same object executes that code. The mutex lock is automatically released when execution exits the synchronized code. Fields, classes and interfaces cannot be declared as <em>synchronized</em>. </dd></dl>
<dl>
<dt><code><a class="mw-redirect" title="This (Java)" href="http://en.wikipedia.org/wiki/This_(Java)">this</a></code>
<dd>Used to represent an instance of the class in which it appears. <code>this</code> can be used to access class members and as a reference to the current instance. The <code>this</code> keyword is also used to forward a call from one constructor in a class to another constructor in the same class. </dd></dl>
<dl>
<dt><code><a title="Exception handling syntax" href="http://en.wikipedia.org/wiki/Exception_handling_syntax#Java">throw</a></code>
<dd>Causes the declared exception instance to be thrown. This causes execution to continue with the first enclosing exception handler declared by the <code>catch</code> keyword to handle an assignment compatible exception type. If no such exception handler is found in the current method, then the method returns and the process is repeated in the calling method. If no exception handler is found in any method call on the stack, then the exception is passed to the thread's uncaught exception handler. </dd></dl>
<dl>
<dt><code><a title="Exception handling syntax" href="http://en.wikipedia.org/wiki/Exception_handling_syntax#Java">throws</a></code>
<dd>Used in method declarations to specify which exceptions are not handled within the method but rather passed to the next higher level of the program. All uncaught exceptions in a method that are not instances of <code>RuntimeException</code> must be declared using the <code>throws</code> keyword. </dd></dl>
<dl>
<dt><code><a title="Transient (computer programming)" href="http://en.wikipedia.org/wiki/Transient_(computer_programming)">transient</a></code>
<dd>Declares that an instance field is not part of the default <a title="Serialization" href="http://en.wikipedia.org/wiki/Serialization">serialized</a> form of an object. When an object is serialized, only the values of its non-transient instance fields are included in the default serial representation. When an object is deserialized, transient fields are initialized only to their default value. </dd></dl>
<dl>
<dt><code><a title="Exception handling syntax" href="http://en.wikipedia.org/wiki/Exception_handling_syntax#Java">try</a></code>
<dd>Defines a block of statements that have exception handling. If an exception is thrown inside the <code>try</code> block, an optional <code>catch</code> block can handle declared exception types. Also, an optional <code>finally</code> block can be declared that will be executed when execution exits the <code>try</code> block and <code>catch</code> clauses, regardless of whether an exception is thrown or not. A <code>try</code> block must have at least one <code>catch</code> clause or a <code>finally</code> block. </dd></dl>
<dl>
<dt><code><a class="mw-redirect" title="Void return type" href="http://en.wikipedia.org/wiki/Void_return_type">void</a></code>
<dd>The <code>void</code> keyword is used to declare that a method does not return any value.<sup class="reference" id="cite_ref-return_4-8"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-return-4"><span>[</span>5<span>]</span></a></sup> </dd></dl>
<dl>
<dd>This keyword is also used as a nonfunctional statement.<sup class="noprint Template-Fact"><span title="This claim needs references to reliable sources&nbsp;since December 2008" style="white-space: nowrap">[<em><a title="Wikipedia:Citation needed" href="http://en.wikipedia.org/wiki/Wikipedia:Citation_needed">citation needed</a></em>]</span></sup> </dd></dl>
<dl>
<dt><code><a title="Volatile variable" href="http://en.wikipedia.org/wiki/Volatile_variable">volatile</a></code>
<dd>Used in field declarations to specify that the variable is modified <a class="mw-redirect" title="Asynchronous" href="http://en.wikipedia.org/wiki/Asynchronous">asynchronously</a> by concurrently running threads. Methods, classes and interfaces thus cannot be declared <em>volatile</em>. </dd></dl>
<dl>
<dt><code id="while"><a title="Do while loop" href="http://en.wikipedia.org/wiki/Do_while_loop#Java">while</a></code>
<dd>The <code>while</code> keyword is used to create a <a title="While loop" href="http://en.wikipedia.org/wiki/While_loop">while loop</a>, which tests a <a title="Boolean expression" href="http://en.wikipedia.org/wiki/Boolean_expression">boolean expression</a> and executes the block of statements associated with the loop if the expression evaluates to <code>true</code>; this continues until the expression evaluates to <code>false</code>. This keyword can also be used to create a <a class="mw-redirect" title="Do-while loop" href="http://en.wikipedia.org/wiki/Do-while_loop">do-while loop</a>; see <em><code><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#do">do</a></code></em>.<sup class="reference" id="cite_ref-do-while_6-1"><a title="" href="http://en.wikipedia.org/wiki/Java_keyword#cite_note-do-while-6"><span>[</span>7<span>]</span></a></sup> </dd></dl>
<img src ="http://www.blogjava.net/yifeng/aggbug/246751.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yifeng/" target="_blank">忆风</a> 2008-12-17 00:09 <a href="http://www.blogjava.net/yifeng/archive/2008/12/17/246751.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用Java Generics简化数据库存取类DAO开发</title><link>http://www.blogjava.net/yifeng/archive/2008/08/24/223940.html</link><dc:creator>忆风</dc:creator><author>忆风</author><pubDate>Sat, 23 Aug 2008 21:39:00 GMT</pubDate><guid>http://www.blogjava.net/yifeng/archive/2008/08/24/223940.html</guid><wfw:comment>http://www.blogjava.net/yifeng/comments/223940.html</wfw:comment><comments>http://www.blogjava.net/yifeng/archive/2008/08/24/223940.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/yifeng/comments/commentRss/223940.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yifeng/services/trackbacks/223940.html</trackback:ping><description><![CDATA[<p><strong>本文介绍怎么样使用Java Generics来简化数据库存取类DAO的开发，并给出了一个完整的例子，大家可以把代码用在实际的开发应用中。</strong></p>
<p>在系统开发中，为了降低耦合性，一般把跟数据库有关的操作封装在被叫做DAO（Dao Access Object）的类里。一般DAO的封装有以下几个原则：</p>
<p>1、一个表操作封装成一个DAO。例如：操作表User的DAO封装为UserDao.java，操作UserRole的DAO封装为UserRole.java等</p>
<p>2、具有良好的接口定义。为了使用简单以及不至于被错误调用，DAO接口必须有具体的型定义。例如：可以直接<br />
User user = userDao.get(userId);取得指定userId的User，而不必进行强制类型转换 User user = (User)userDao.get(userId); [X]<br />
为了避免误操作，userDao.delete(UserRole);应该在编译期就报错。</p>
<p>为了对DAO进行良好的封装，JDK1.5之前，DAO开发是一个大量重复代码的体力过程；JDK1.5开始引入了Generics概念，我们可以利用JDK1.5的Generics特性大大简化DAO开发。</p>
<p>Generics概念</p>
<p>Java的Generics就是Java的泛型，类似于C++的模板概念，Generics介绍起来比较复杂，简单说来，泛型从语言特性的底层上支持，可以让你为某些相似功能定义一个具有统一接口的通用模板，该模板可以接收任意类型并应用到类成员变量的类型，方法的参数类型，方法的返回值类型等；并可以在编译期支持类型的安全转换。举例说：<br />
java.util.List是一个泛型，一个定义为List&lt;User&gt; userList;的变量userList，可以通过User user = userList.get(0);便可直接得到User，不用经过类型的强制转换了。</p>
<p>Generics更详细的介绍请参考相关文章，这里不做详述。</p>
<p>下面举例我们给出2种DAO的封装方法，一种是不使用Java Generics特性的一般封装方法；一种是使用泛型的封装方法。通过比较，我们会发现Java的Generics特性到底为我们简化了哪些工作。</p>
<h4>使用Java Generics特性封装DAO</h4>
<table class="content" id="table2" cellspacing="1" cellpadding="2" bgcolor="#999999">
    <tbody>
        <tr class="altEventRow" bgcolor="#ffffff">
            <th>文件名</th>
            <th>说明</th>
        </tr>
        <tr class="eventRow" bgcolor="#ffffff">
            <td>IDao.java</td>
            <td>dao的顶层抽象接口</td>
        </tr>
        <tr class="altEventRow" bgcolor="#ffffff">
            <td>AbstractDao.java</td>
            <td>IDao的实现类，实现了get/update/delete等基本操作</td>
        </tr>
        <tr class="eventRow" bgcolor="#ffffff">
            <td>User.java</td>
            <td>USER表对应的entity定义</td>
        </tr>
        <tr class="altEventRow" bgcolor="#ffffff">
            <td>IUserDao.java</td>
            <td>USER表操作DAO接口定义</td>
        </tr>
        <tr class="eventRow" bgcolor="#ffffff">
            <td>UserDao.java</td>
            <td>USER表操作DAO实现</td>
        </tr>
        <tr class="altEventRow" bgcolor="#ffffff">
            <td>HelloGenerics.java</td>
            <td>测试类</td>
        </tr>
    </tbody>
</table>
<p>IDao.java</p>
<table class="content" bordercolor="#ffffcc" height="63" cellspacing="0" cellpadding="0" width="81%" bgcolor="#f9f9f9" border="0">
    <tbody>
        <tr>
            <td>// super dao interface<br />
            <span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">interface</span> IDao &lt;T, PK <span style="font-weight: bold; color: #7f0055">extends</span> Serializable&gt; {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> T get(PK pk);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> List &lt;T&gt;getAll();<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> PK save(T entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> update(T entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> saveOrUpdate(T entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> delete(T entity);<br />
            } </td>
        </tr>
    </tbody>
</table>
<p>AbstractDao.java </p>
<table class="content" bordercolor="#ffffcc" height="63" cellspacing="0" cellpadding="0" width="81%" bgcolor="#f9f9f9" border="0">
    <tbody>
        <tr>
            <td>//super abstract dao class<br />
            </span><span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">abstract</span> <span style="font-weight: bold; color: #7f0055">class</span> AbstractDao &lt;T, PK <span style="font-weight: bold; color: #7f0055">extends</span> Serializable&gt;<span style="font-weight: bold; color: #7f0055">implements</span> IDao&lt;T, PK&gt; {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">private</span> Class clz;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> AbstractDao(Class clz) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.clz = clz;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> T get(PK pk) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">return</span> (T)getSession().get(clz, pk);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> List &lt;T&gt;getAll() {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">return</span> getSession().createCriteria(clz).list();<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> PK save(T entity) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">return</span> (PK)getSession().save(entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> update(T entity) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getSession().update(entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> saveOrUpdate(T entity) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getSession().saveOrUpdate(entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> delete(T entity) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getSession().delete(entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">private</span> Session session;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">protected</span> Session getSession() {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #3f7f5f">//wrap session in a class such as HibernateUtils, then you can use&nbsp;&nbsp;HibernateUtils.getCurrentSession() for getting a session.<br />
            </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">return</span> session;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            } </td>
        </tr>
    </tbody>
</table>
<p>User.java</p>
<table class="content" bordercolor="#ffffcc" height="63" cellspacing="0" cellpadding="0" width="81%" bgcolor="#f9f9f9" border="0">
    <tbody>
        <tr>
            <td><span style="color: #3f7f5f">//persist entity<br />
            </span><span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">class</span> User {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">private</span> String id;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">private</span> String name;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> String getId() {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">return</span> id;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> setId(String id) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.id = id;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> String getName() {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">return</span> name;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> setName(String name) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.name = name;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #3f7f5f">//... setter/getter HERE<br />
            </span>} </td>
        </tr>
    </tbody>
</table>
<p>IUserDao.java</p>
<table class="content" bordercolor="#ffffcc" height="63" cellspacing="0" cellpadding="0" width="81%" bgcolor="#f9f9f9" border="0">
    <tbody>
        <tr>
            <td><span style="color: #3f7f5f">//User Dao interface<br />
            </span><span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">interface</span> IUserDao <span style="font-weight: bold; color: #7f0055">extends</span> IDao &lt;User, String&gt; {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #3f7f5f">// all are empty<br />
            </span>} </td>
        </tr>
    </tbody>
</table>
<p>UserDao.java</p>
<table class="content" bordercolor="#ffffcc" height="63" cellspacing="0" cellpadding="0" width="81%" bgcolor="#f9f9f9" border="0">
    <tbody>
        <tr>
            <td><span style="color: #3f7f5f">//UserDao Implementation<br />
            </span><span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">class</span> UserDao <span style="font-weight: bold; color: #7f0055">extends</span> AbstractDao &lt;User, String&gt; <span style="font-weight: bold; color: #7f0055">implements</span> IUserDao {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> UserDao() {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">super</span>(User.<span style="font-weight: bold; color: #7f0055">class</span>);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #3f7f5f">// or use the following constructor<br />
            </span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #3f7f5f">//public UserDao(Class&lt;User&gt; type) {<br />
            </span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #3f7f5f">//&nbsp;&nbsp;&nbsp;&nbsp;super(type);<br />
            </span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #3f7f5f">//}<br />
            </span>}</td>
        </tr>
    </tbody>
</table>
<p>HelloGenerics.java</p>
<table class="content" bordercolor="#ffffcc" height="63" cellspacing="0" cellpadding="0" width="81%" bgcolor="#f9f9f9" border="0">
    <tbody>
        <tr>
            <td><span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">class</span> HelloGenerics {<br />
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">static</span> <span style="font-weight: bold; color: #7f0055">void</span> main(String[] args) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IUserDao userDao = <span style="font-weight: bold; color: #7f0055">new</span> UserDao();<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User user = userDao.get("<span style="color: #2a00ff">1</span>");<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">if</span> (user != <span style="font-weight: bold; color: #7f0055">null</span>) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(user.getName());<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;User&gt; userList = userDao.getAll();<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">for</span> (User u:userList) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(user.getName());<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            } </td>
        </tr>
    </tbody>
</table>
<p>我们可以看到IUserDao.java与UserDao.java非常简单。</p>
<h4>不使用Java Generics特性DAO的封装</h4>
<p>同样，我们给出不使用Java Generics特性DAO的封装。该封装也是一个具有良好结构性的封装。</p>
<p>文件列表2：</p>
<table class="content" id="table3" cellspacing="1" cellpadding="1" bgcolor="#999999">
    <tbody>
        <tr class="eventRow" bgcolor="#ffffff">
            <th>文件名</th>
            <th>说明</th>
        </tr>
        <tr class="altEventRow" bgcolor="#ffffff">
            <td>DaoUtils.java</td>
            <td>dao通用工具类，提供get/update/delete等基本操作</td>
        </tr>
        <tr class="eventRow" bgcolor="#ffffff">
            <td>User.java</td>
            <td>USER表对应的entity定义</td>
        </tr>
        <tr class="altEventRow" bgcolor="#ffffff">
            <td>IUserDao.java</td>
            <td>USER表操作DAO接口定义</td>
        </tr>
        <tr class="eventRow" bgcolor="#ffffff">
            <td>UserDao.java</td>
            <td>USER表操作DAO实现</td>
        </tr>
        <tr class="altEventRow" bgcolor="#ffffff">
            <td>HelloGenerics.java</td>
            <td>测试类</td>
        </tr>
    </tbody>
</table>
<p>DaoUtils.java</p>
<table class="content" bordercolor="#ffffcc" height="63" cellspacing="0" cellpadding="0" width="81%" bgcolor="#f9f9f9" border="0">
    <tbody>
        <tr>
            <td><span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">class</span> DaoUtils {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">static</span> Object get(Class clz, Serializable pk) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">return</span> getSession().get(clz, pk);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">static</span> List getAll(Class clz) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">return</span> getSession().createCriteria(clz).list();<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">static</span> Serializable save(Object entity) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">return</span> getSession().save(entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">static</span> <span style="font-weight: bold; color: #7f0055">void</span> update(Object entity) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getSession().update(entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">static</span> <span style="font-weight: bold; color: #7f0055">void</span> saveOrUpdate(Object entity) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getSession().saveOrUpdate(entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">static</span> <span style="font-weight: bold; color: #7f0055">void</span> delete(Object entity) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getSession().delete(entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">private</span> <span style="font-weight: bold; color: #7f0055">static</span> Session session;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">protected</span> <span style="font-weight: bold; color: #7f0055">static</span> Session getSession() {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #3f7f5f">//wrap session in a class such as HibernateUtils, then you can use&nbsp;&nbsp;HibernateUtils.getCurrentSession() for getting a session.<br />
            </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">return</span> session;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            } </td>
        </tr>
    </tbody>
</table>
<p>User.java</p>
<table class="content" bordercolor="#ffffcc" height="63" cellspacing="0" cellpadding="0" width="81%" bgcolor="#f9f9f9" border="0">
    <tbody>
        <tr>
            <td><span style="color: #3f7f5f">//persist entity<br />
            </span><span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">class</span> User {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">private</span> String id;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">private</span> String name;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> String getId() {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">return</span> id;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> setId(String id) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.id = id;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> String getName() {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">return</span> name;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> setName(String name) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.name = name;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #3f7f5f">//... setter/getter HERE<br />
            </span>} </td>
        </tr>
    </tbody>
</table>
<p>IUserDao.java</p>
<table class="content" bordercolor="#ffffcc" height="63" cellspacing="0" cellpadding="0" width="81%" bgcolor="#f9f9f9" border="0">
    <tbody>
        <tr>
            <td><span style="color: #3f7f5f">//User Dao interface<br />
            </span><span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">interface</span> IUserDao {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> User get(String pk);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> List getAll();<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> String save(User entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> update(User entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> saveOrUpdate(User entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> delete(User entity);<br />
            } </td>
        </tr>
    </tbody>
</table>
<p>UserDao.java</p>
<table class="content" bordercolor="#ffffcc" height="63" cellspacing="0" cellpadding="0" width="81%" bgcolor="#f9f9f9" border="0">
    <tbody>
        <tr>
            <td><span style="color: #3f7f5f">//UserDao Implementation<br />
            </span><span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">class</span> UserDao <span style="font-weight: bold; color: #7f0055">implements</span> IUserDao {<br />
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> User get(String pk) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">return</span> (User)DaoUtils.get(User.class, pk);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> List getAll() {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">return</span> DaoUtils.getAll(User.<span style="font-weight: bold; color: #7f0055">class</span>);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> String save(User entity) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">return</span> (String)DaoUtils.save(entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> update(User entity) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DaoUtils.update(entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> saveOrUpdate(User entity) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DaoUtils.saveOrUpdate(entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">void</span> delete(User entity) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DaoUtils.delete(entity);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            } </td>
        </tr>
    </tbody>
</table>
<p>HelloGenerics.java</p>
<table class="content" bordercolor="#ffffcc" height="63" cellspacing="0" cellpadding="0" width="81%" bgcolor="#f9f9f9" border="0">
    <tbody>
        <tr>
            <td><span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">class</span> HelloGenerics {<br />
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">public</span> <span style="font-weight: bold; color: #7f0055">static</span> <span style="font-weight: bold; color: #7f0055">void</span> main(String[] args) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IUserDao userDao = <span style="font-weight: bold; color: #7f0055">new</span> UserDao();<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User user = userDao.get("<span style="color: #2a00ff">1</span>");<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">if</span> (user != <span style="font-weight: bold; color: #7f0055">null</span>) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(user.getName());<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;User&gt; userList = userDao.getAll();<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold; color: #7f0055">for</span> (User u:userList) {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(user.getName());<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
            &nbsp;&nbsp;&nbsp;&nbsp;}<br />
            }</td>
        </tr>
    </tbody>
</table>
<p>我们注意到IUserDao.java与UserDao.java的实现比使用Java Generics特性的情况要复杂多了。</p>
<img src ="http://www.blogjava.net/yifeng/aggbug/223940.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yifeng/" target="_blank">忆风</a> 2008-08-24 05:39 <a href="http://www.blogjava.net/yifeng/archive/2008/08/24/223940.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>