﻿<?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-EricGu's Java-Record-Space-随笔分类-Java</title><link>http://www.blogjava.net/EricGu/category/38212.html</link><description>专注 学习 实践 创新</description><language>zh-cn</language><lastBuildDate>Wed, 01 Jul 2009 04:34:01 GMT</lastBuildDate><pubDate>Wed, 01 Jul 2009 04:34:01 GMT</pubDate><ttl>60</ttl><item><title>Java语法总结 - 方法</title><link>http://www.blogjava.net/EricGu/archive/2009/06/30/284798.html</link><dc:creator>Eric Gu</dc:creator><author>Eric Gu</author><pubDate>Tue, 30 Jun 2009 09:17:00 GMT</pubDate><guid>http://www.blogjava.net/EricGu/archive/2009/06/30/284798.html</guid><wfw:comment>http://www.blogjava.net/EricGu/comments/284798.html</wfw:comment><comments>http://www.blogjava.net/EricGu/archive/2009/06/30/284798.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/EricGu/comments/commentRss/284798.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/EricGu/services/trackbacks/284798.html</trackback:ping><description><![CDATA[一、方法的重写。<br />
<br />
1、重写只能出现在继承关系之中。当一个类继承它的父类方法时，都有机会重写该父类的方法。一个特例是父类的方法被标识为final。重写的主要优点是能够定义某个子类型特有的行为。<br />
&nbsp;&nbsp; &nbsp;class Animal {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public void eat(){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("Animal is eating.");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;class Horse extends Animal{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public void eat(){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("Horse is eating.");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
<br />
2、对于从父类继承来的抽象方法，要么在子类用重写的方式设计该方法，要么把子类也标识为抽象的。所以抽象方法可以说是必须要被重写的方法。<br />
<br />
3、重写的意义。<br />
重写方法可以实现多态，用父类的引用来操纵子类对象，但是在实际运行中对象将运行其自己特有的方法。<br />
&nbsp;&nbsp; &nbsp;public class Test {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public static void main (String[] args) {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Animal h = new Horse();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;h.eat();&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
<br />
&nbsp;&nbsp; &nbsp;class Animal {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public void eat(){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("Animal is eating.");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;class Horse extends Animal{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public void eat(){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("Horse is eating.");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public void buck(){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
<br />
一个原则是：使用了什么引用，编译器就会只调用引用类所拥有的方法。如果调用子类特有的方法，如上例的h.buck(); 编译器会抱怨的。也就是说，编译器只看引用类型，而不是对象类型。<br />
<br />
4、重写方法的规则。<br />
若想实现一个合格重写方法，而不是重载，那么必须同时满足下面的要求！<br />
<br />
A、重写规则之一：重写方法不能比被重写方法限制有更严格的访问级别。<br />
（但是可以更广泛，比如父类方法是包访问权限，子类的重写方法是public访问权限。）<br />
比如：Object类有个toString()方法，开始重写这个方法的时候我们总容易忘记public修饰符，编译器当然不会放过任何教训我们的机会。出错的原因就是：没有加任何访问修饰符的方法具有包访问权限，包访问权限比public当然要严格了，所以编译器会报错的。<br />
<br />
B、重写规则之二：参数列表必须与被重写方法的相同。<br />
重写有个孪生的弟弟叫重载，也就是后面要出场的。如果子类方法的参数与父类对应的方法不同，那么就是你认错人了，那是重载，不是重写。<br />
<br />
C、重写规则之三：返回类型必须与被重写方法的返回类型相同。<br />
父类方法A：void eat(){}&nbsp; 子类方法B：int eat(){}&nbsp; 两者虽然参数相同，可是返回类型不同，所以不是重写。<br />
父类方法A：int eat(){}&nbsp;&nbsp; 子类方法B：long eat(){}&nbsp; 返回类型虽然兼容父类，但是不同就是不同，所以不是重写。<br />
<br />
D、重写规则之四：重写方法不能抛出新的异常或者比被重写方法声明的检查异常更广的检查异常。但是可以抛出更少，更有限或者不抛出异常。<br />
&nbsp;&nbsp; &nbsp;import java.io.*;<br />
&nbsp;&nbsp; &nbsp;public class Test {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public static void main (String[] args) {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Animal h = new Horse();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;try {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;h.eat();&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;catch (Exception e) {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
<br />
&nbsp;&nbsp; &nbsp;class Animal {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public void eat() throws Exception{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("Animal is eating.");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;throw new Exception();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;class Horse extends Animal{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public void eat() throws IOException{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("Horse is eating.");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;throw new IOException();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
这个例子中，父类抛出了检查异常Exception，子类抛出的IOException是Exception的子类，也即是比被重写的方法抛出了更有限的异常，这是可以的。如果反过来，父类抛出IOException，子类抛出更为宽泛的Exception，那么不会通过编译的。<br />
注意：这种限制只是针对检查异常，至于运行时异常RuntimeException及其子类不再这个限制之中。<br />
<br />
E、重写规则之五：不能重写被标识为final的方法。<br />
<br />
F、重写规则之六：如果一个方法不能被继承，则不能重写它。<br />
比较典型的就是父类的private方法。下例会产生一个有趣的现象。<br />
&nbsp;&nbsp; &nbsp;public class Test {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public static void main (String[] args) {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//Animal h = new Horse();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Horse h = new Horse();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;h.eat();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
<br />
&nbsp;&nbsp; &nbsp;class Animal {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;private void eat(){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("Animal is eating.");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;class Horse extends Animal{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public void eat(){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("Horse is eating.");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
这段代码是能通过编译的。表面上看来违反了第六条规则，但实际上那是一点巧合。Animal类的eat()方法不能被继承，因此Horse类中的eat()方法是一个全新的方法，不是重写也不是重载，只是一个只属于Horse类的全新的方法！这点让很多人迷惑了，但是也不是那么难以理解。<br />
main()方法如果是这样：<br />
&nbsp;&nbsp; &nbsp;Animal h = new Horse();<br />
&nbsp;&nbsp; &nbsp;//Horse h = new Horse();<br />
&nbsp;&nbsp; &nbsp;h.eat();<br />
编译器会报错，为什么呢？Horse类的eat()方法是public的啊！应该可以调用啊！请牢记，多态只看父类引用的方法，而不看子类对象的方法！<br />
<br />
<br />
二、方法的重载。<br />
重载是有好的，它不要求你在调用一个方法之前转换数据类型，它会自动地寻找匹配的方法。方法的重载是在编译时刻就决定调用哪个方法了，和重写不同。最最常用的地方就是构造器的重载。<br />
<br />
1、基本数据类型参数的重载。<br />
&nbsp;&nbsp; &nbsp;public class Test {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;static void method(byte b){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("method:byte");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;static void method(short s){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("method:short");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;static void method(int i){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("method:int");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;static void method(float f){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("method:float");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;static void method(double d){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("method:double");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public static void main (String[] args) {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;method((byte)1);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;method('c');<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;method(1);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;method(1L);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;method(1.1);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;method(1.1f);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
输出结果：<br />
method:byte<br />
method:int<br />
method:int<br />
method:float<br />
method:double<br />
method:float<br />
<br />
可以看出：首先要寻找的是数据类型正好匹配方法。如果找不到，那么就提升为表达能力更强的数据类型，如上例没有正好容纳long的整数类型，那么就转换为float类型的。如果通过提升也不能找到合适的兼容类型，那么编译器就会报错。反正是不会自动转换为较小的数据类型的，必须自己强制转换，自己来承担转变后果。<br />
<br />
char类型比较特殊，如果找不到正好匹配的类型，它会转化为int而不是short，虽然char是16位的。<br />
<br />
<br />
2、重载方法的规则。<br />
<br />
A、被重载的方法必须改变参数列表。<br />
参数必须不同，这是最重要的！不同有两个方面，参数的个数，参数的类型，参数的顺序。<br />
<br />
B、被重载的方法与返回类型无关。<br />
也就是说，不能通过返回类型来区分重载方法。<br />
<br />
C、被重载的方法可以改变访问修饰符。<br />
没有重写方法那样严格的限制。<br />
<br />
D、被重载的方法可以声明新的或者更广的检查异常。<br />
没有重写方法那样严格的限制。<br />
<br />
E、方法能够在一个类中或者在一个子类中被重载。<br />
<br />
<br />
3、带对象引用参数的方法重载。<br />
&nbsp;&nbsp; &nbsp;class Animal {}<br />
&nbsp;&nbsp; &nbsp;class Horse extends Animal{}<br />
&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;public class Test {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;static void method(Animal a){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("Animal is called.");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;static void method(Horse h){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("Horse is called.");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public static void main (String[] args) {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Animal a = new Animal();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Horse h = new Horse();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Animal ah = new Horse();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;method(a);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;method(h);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;method(ah);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp; &nbsp;}<br />
输出结果是：<br />
Animal is called.<br />
Horse is called.<br />
Animal is called.<br />
前两个输出没有任何问题。第三个方法为什么不是输出&#8220;Horse is called.&#8221;呢？还是那句老话，要看引用类型而不是对象类型，方法重载是在编译时刻就决定的了，引用类型决定了调用哪个版本的重载方法。<br />
<br />
<br />
4、重载和重写方法区别的小结。<br />
如果能彻底弄明白下面的例子，说明你对重载和重写非常了解了，可以结束这节的复习了。<br />
&nbsp;&nbsp; &nbsp;class Animal {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public void eat(){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("Animal is eating.");&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;class Horse extends Animal{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public void eat(){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("Horse is eating.");&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public void eat(String food){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println ("Horse is eating " + food);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;public class Test {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public static void main (String[] args) {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Animal a = new Animal();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Horse h = new Horse();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Animal ah = new Horse();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;a.eat();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;h.eat();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;h.eat("apple");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ah.eat();<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//a.eat("apple");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//ah.eat("apple");<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp; &nbsp;}<br />
<br />
四个输出分别是什么？被注释的两条语句为什么不能通过编译？<br />
第一条：a.eat(); 普通的方法调用，没有多态，没什么技术含量。调用了Animal类的eat()方法，输出：Animal is eating.<br />
第二条：h.eat(); 普通的方法调用，也没什么技术含量。调用了Horse类的eat()方法，输出：Horse is eating.<br />
第三条：h.eat("apple"); 重载。Horse类的两个eat()方法重载。调用了Horse类的eat(String food)方法，输出：Horse is eating apple<br />
第四条：ah.eat(); 多态。前面有例子了，不难理解。输出：Horse is eating.<br />
第五条：a.eat("apple"); 低级的错误，Animal类中没有eat(String food)方法。因此不能通过编译。<br />
第六条：ah.eat("apple"); 关键点就在这里。解决的方法还是那句老话，不能看对象类型，要看引用类型。Animal类中没有eat(String food)方法。因此不能通过编译。<br />
<br />
小结一下：多态不决定调用哪个重载版本；多态只有在决定哪个重写版本时才起作用。<br />
重载对应编译时，重写对应运行时。够简洁的了吧！<br />
<br />
<br />
三、构造方法。<br />
构造方法是一种特殊的方法，没有构造方法就不能创建一个新对象。实际上，不仅要调用对象实际类型的构造方法，还要调用其父类的构造方法，向上追溯，直到Object类。构造方法不必显式地调用，当使用new关键字时，相应的构造方法会自动被调用。<br />
<br />
1、构造方法的规则。<br />
A、构造方法能使用任何访问修饰符。包括private，事实上java类库有很多都是这样的，设计者不希望使用者创建该类的对象。<br />
<br />
B、构造方法的名称必须与类名相同。这样使得构造方法与众不同，如果我们遵守sun的编码规范，似乎只有构造方法的首字母是大写的。<br />
<br />
C、构造方法不能有返回类型。<br />
反过来说，有返回类型的不是构造方法<br />
&nbsp;&nbsp; &nbsp;public class Test {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;int Test(){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return 1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
这个方法是什么东西？一个冒充李逵的李鬼而已，int Test()和其他任何普通方法没什么两样，就是普通的方法！只不过看起来很恶心，类似恶心的东西在考试卷子里比较多。<br />
<br />
D、如果不在类中创建自己的构造方法，编译器会自动生成默认的不带参数的构造函数。<br />
这点很容易验证！写一个这样简单的类，编译。<br />
class Test {<br />
}<br />
对生成的Test.class文件反编译：javap Test，可以看到：<br />
D:"JavaCode"bin&gt;javap Test<br />
Compiled from "Test.java"<br />
class Test extends java.lang.Object{<br />
&nbsp;&nbsp;&nbsp; Test();<br />
}<br />
看到编译器自动添加的默认构造函数了吧！<br />
<br />
E、如果只创建了带参数的构造方法，那么编译器不会自动添加无参的构造方法的！<br />
<br />
F、在每个构造方法中，如果使用了重载构造函数this()方法，或者父类的构造方法super()方法，那么this()方法或者super()方法必须放在第一行。而且这两个方法只能选择一个，因此它们之间没有顺序问题。<br />
<br />
G、除了编译器生成的构造方法，而且没有显式地调用super()方法，那么编译器会插入一个super()无参调用。<br />
<br />
H、抽象类有构造方法。<br />
<br />
<br />
四、静态方法的重载与重写（覆盖）。<br />
<br />
1、静态方法是不能被覆盖的。可以分两种情况讨论：<br />
<br />
A、子类的非静态方法&#8220;覆盖&#8221;父类的静态方法。<br />
这种情况下，是不能通过编译的。<br />
<br />
<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;Father{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;print(){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println&nbsp;(</span><span style="color: #000000">"</span><span style="color: #000000">in&nbsp;father&nbsp;&nbsp;method</span><span style="color: #000000">"</span><span style="color: #000000">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;Child&nbsp;</span><span style="color: #0000ff">extends</span><span style="color: #000000">&nbsp;Father{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;print(){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println&nbsp;(</span><span style="color: #000000">"</span><span style="color: #000000">in&nbsp;child&nbsp;method</span><span style="color: #000000">"</span><span style="color: #000000">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</span></div>
<br />
static方法表示该方法不关联具体的类的对象，可以通过类名直接调用，也就是编译的前期就绑定了，不存在后期动态绑定，也就是不能实现多态。子类的非静态方法是与具体的对象绑定的，两者有着不同的含义。<br />
<br />
B、子类的静态方法&#8220;覆盖&#8221;父类静态方法。<br />
这个覆盖依然是带引号的。事实上把上面那个例子Child类的print方法前面加上static修饰符，确实能通过编译！但是不要以为这就是多态！多态的特点是动态绑定，看下面的例子：<br />
<br />
<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;Father{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;print(){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println&nbsp;(</span><span style="color: #000000">"</span><span style="color: #000000">in&nbsp;father&nbsp;&nbsp;method</span><span style="color: #000000">"</span><span style="color: #000000">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;Child&nbsp;</span><span style="color: #0000ff">extends</span><span style="color: #000000">&nbsp;Father{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;print(){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println&nbsp;(</span><span style="color: #000000">"</span><span style="color: #000000">in&nbsp;child&nbsp;method</span><span style="color: #000000">"</span><span style="color: #000000">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
<br />
</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;Test{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;main&nbsp;(String[]&nbsp;args)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Father&nbsp;f&nbsp;</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;Child();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.print();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</span></div>
<br />
输出结果是：in father&nbsp; method<br />
从这个结果可以看出，并没有实现多态。<br />
但是这种形式很迷惑人，貌似多态，实际编程中千万不要这样搞，会把大家搞懵的！<br />
它不符合覆盖表现出来的特性，不应该算是覆盖！<br />
总而言之，静态方法不能被覆盖。<br />
<br />
2、静态方法可以和非静态方法一样被重载。<br />
这样的例子太多了，我不想写例程了。看看java类库中很多这样的例子。<br />
如java.util.Arrays类的一堆重载的binarySearch方法。<br />
在这里提一下是因为查资料时看到这样的话&#8220;sun的SL275课程说，静态方法只能控制静态变量（他们本身没有），静态方法不能被重载和覆盖&#8230;&#8230;&#8221; <br />
大家不要相信啊！可以重载的。而且静态与非静态方法可以重载。<br />
<br />
从重载的机制很容易就理解了，重载是在编译时刻就决定的了，非静态方法都可以，静态方法怎么可能不会呢？<br />
<img src ="http://www.blogjava.net/EricGu/aggbug/284798.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/EricGu/" target="_blank">Eric Gu</a> 2009-06-30 17:17 <a href="http://www.blogjava.net/EricGu/archive/2009/06/30/284798.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java语法总结 - 字符串</title><link>http://www.blogjava.net/EricGu/archive/2009/06/30/284780.html</link><dc:creator>Eric Gu</dc:creator><author>Eric Gu</author><pubDate>Tue, 30 Jun 2009 07:34:00 GMT</pubDate><guid>http://www.blogjava.net/EricGu/archive/2009/06/30/284780.html</guid><wfw:comment>http://www.blogjava.net/EricGu/comments/284780.html</wfw:comment><comments>http://www.blogjava.net/EricGu/archive/2009/06/30/284780.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/EricGu/comments/commentRss/284780.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/EricGu/services/trackbacks/284780.html</trackback:ping><description><![CDATA[Java的String太特别了，也太常用了，所以重要。我初学Java就被它搞蒙了，太多混淆的概念了，比如它的不变性。所以必须深入机制地去理解它。<br />
<br />
<br />
1、String中的每个字符都是一个16位的Unicode字符，用Unicode很容易表达丰富的国际化字符集，比如很好的中文支持。甚至Java的标识符都可以用汉字，但是没人会用吧（只在一本清华的《Java2实用教程》看过）。<br />
<br />
2、判断空字符串。根据需要自己选择某个或者它们的组合<br />
&nbsp;&nbsp; &nbsp;if ( s == null )&nbsp;&nbsp; &nbsp;//从引用的角度<br />
&nbsp;&nbsp; &nbsp;if ( s.length() == 0 ) &nbsp;&nbsp; &nbsp;//从长度判别<br />
&nbsp;&nbsp; &nbsp;if ( s.trim().length () == 0 ) &nbsp;&nbsp; &nbsp;//是否有多个空白字符<br />
trim()方法的作用是是移除前导和尾部的Unicode值小于'"u0020'的字符，并返回&#8220;修剪&#8221;好的字符串。这种方法很常用，比如需要用户输入用户名，用户不小心加了前导或者尾部空格，一个好的程序应该知道用户不是故意的，即使是故意的也应该智能点地处理。<br />
判断空串是很常用的操作，但是Java类库直到1.6才提供了isEmpty()方法。当且仅当 length() 为 0 时返回 true。<br />
<br />
3、未初始化、空串""与null。它们是不同的概念。对未初始化的对象操作会被编译器挡在门外；null是一个特殊的初始化值，是一个不指向任何对象的引用，对引用为null的对象操作会在运行时抛出异常NullPointerException；而空串是长度为0的字符串，和别的字符串的唯一区别就是长度为0。<br />
例子：<br />
&nbsp;&nbsp; &nbsp;public class StringTest{<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;static String s1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public static void main(String[] args) {<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String s2;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String s3 = "";<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.print(s1.isEmpty());&nbsp;&nbsp; &nbsp; //运行时异常<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.print(s2.isEmpty());&nbsp;&nbsp; &nbsp; //编译出错<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.print(s3.isEmpty());&nbsp;&nbsp; &nbsp; //ok！输出true<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
<br />
4、String类的方法很多，在编写相关代码的时候看看JDK文档时有好处的，要不然花了大量时间实现一个已经存在的方法是很不值得的，因为编写、测试、维护自己的代码使项目的成本增加，利润减少，严重的话会导致开不出工资&#8230;&#8230;<br />
<br />
5、字符串的比较。<br />
Java不允许自定义操作符重载，因此字符串的比较要用compareTo() 或者 compareToIgnoreCase()。s1.compareTo(s2)，返回值大于0则，则前者大；等于0，一般大；小于0，后者大。比较的依据是字符串中各个字符的Unicode值。<br />
<br />
6、toString()方法。<br />
Java的任何对象都有toString()方法，是从Object对象继承而来的。它的作用就是让对象在输出时看起来更有意义，而不是奇怪的对象的内存地址。对测试也是很有帮助的。<br />
<br />
7、String对象是不变的！可以变化的是String对象的引用。<br />
String name = "ray";<br />
name.concat("long");&nbsp; //字符串连接<br />
System.out.println(name); //输出name，ok，还是"ray"<br />
name = name.concat("long");&nbsp; //把字符串对象连接的结果赋给了name引用<br />
System.out.println(name);&nbsp; //输出name，oh！，变成了"raylong"<br />
上述三条语句其实产生了3个String对象，"ray"，"long"，"raylong"。第2条语句确实产生了"raylong"字符串，但是没有指定把该字符串的引用赋给谁，因此没有改变name引用。第3条语句根据不变性，并没有改变"ray"，JVM创建了一个新的对象，把"ray"，"long"的连接赋给了name引用，因此引用变了，但是原对象没变。<br />
<br />
8、String的不变性的机制显然会在String常量内有大量的冗余。如："1" + "2" + "3" +......+ "n" 产生了n+(n+1)个String对象！因此Java为了更有效地使用内存，JVM留出一块特殊的内存区域，被称为&#8220;String常量池&#8221;。对String多么照顾啊！当编译器遇见String常量的时候，它检查该池内是否已经存在相同的String常量。如果找到，就把新常量的引用指向现有的String，不创建任何新的String常量对象。<br />
<br />
那么就可能出现多个引用指向同一个String常量，会不会有别名的危险呢？No problem！String对象的不变性可以保证不会出现别名问题！这是String对象与普通对象的一点区别。<br />
<br />
乍看起来这是底层的机制，对我们编程没什么影响。而且这种机制会大幅度提高String的效率，实际上却不是这样。为连接n个字符串使用字符串连接操作时，要消耗的时间是n的平方级！因为每两个字符串连接，它们的内容都要被复制。因此在处理大量的字符串连接时，而且要求性能时，我们不要用String，StringBuffer是更好的选择。<br />
<br />
8、StringBuffer类。StringBuffer类是可变的，不会在字符串常量池中，而是在堆中，不会留下一大堆无用的对象。而且它可将字符串缓冲区安全地用于多个线程。每个StringBuffer对象都有一定的容量。只要StringBuffer对象所包含的字符序列的长度没有超出此容量，就无需分配新的内部缓冲区数组。如果内部缓冲区溢出，则此容量自动增大。这个固定的容量是16个字符。我给这种算法起个名字叫&#8220;添饭算法&#8221;。先给你一满碗饭，不够了再给你一满碗饭。<br />
例子：<br />
&nbsp;&nbsp; &nbsp;StringBuffer sb = new StringBuffer();&nbsp;&nbsp; &nbsp;//初始容量为 16 个字符<br />
&nbsp;&nbsp; &nbsp;sb.append("1234");&nbsp;&nbsp; &nbsp;//这是4个字符，那么16个字符的容量就足够了，没有溢出<br />
&nbsp;&nbsp; &nbsp;System.out.println(sb.length());&nbsp;&nbsp; &nbsp;//输出字符串长度是4<br />
&nbsp;&nbsp; &nbsp;System.out.println(sb.capacity());&nbsp;&nbsp; &nbsp;//输出该字符串缓冲区的容量是16<br />
<br />
&nbsp;&nbsp; &nbsp;sb.append("12345678901234567");&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//这是17个字符，16个字符的容量不够了，扩容为17+16个字符的容量<br />
&nbsp;&nbsp; &nbsp;System.out.println(sb.length());&nbsp;&nbsp; &nbsp;//输出字符串长度是17<br />
&nbsp;&nbsp; &nbsp;System.out.println(sb.capacity());&nbsp;&nbsp; &nbsp;//输出该字符串缓冲区的容量是34<br />
<br />
&nbsp;&nbsp; &nbsp;sb.append("890").reverse().insert(10,"-");&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;System.out.println(sb);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//输出0987654321-09876543214321<br />
<br />
字符串的长度和字符缓冲区的容量是两个概念，注意区别。<br />
还有串联的方式看起来是不是很酷！用返回值连接起来可以实现这种简洁和优雅。<br />
<br />
10、StringBuilder类。 从J2SE 5.0 提供了StringBuilder类，它和StringBuffer类是孪生兄弟，很像。它存在的价值在于：对字符串操作的效率更高。不足的是线程安全无法保证，不保证同步。那么两者性能到底差多少呢？很多！<br />
请参阅：http://book.csdn.net/bookfiles/135/1001354628.shtml<br />
实践：<br />
单个线程的时候使用StringBuilder类，以提高效率，而且它的API和StringBuffer兼容，不需要额外的学习成本，物美价廉。多线程时使用StringBuffer，以保证安全。<br />
<br />
11、字符串的比较。<br />
下面这条可能会让你晕，所以你可以选择看或者不看。它不会对你的职业生涯造成任何影响。而且谨记一条，比较字符串要用equals()就ok了！一旦用了&#8220;==&#8221;就会出现很怪异的现象。之所以把这部分放在最后，是想节省大家的时间，因为这条又臭又长。推荐三种人：一、没事闲着型。二、想深入地理解Java的字符串，即使明明知道学了也没用。三、和我一样爱好研究&#8220;茴&#8221;字有几种写法。<br />
<br />
还是那句老话，String太特殊了，以至于某些规则对String不起作用。个人感觉这种特殊性并不好。看例子：<br />
例子A：<br />
&nbsp;&nbsp; &nbsp;String str1 = "java";<br />
&nbsp;&nbsp; &nbsp;String str2 = "java";<br />
&nbsp;&nbsp; &nbsp;System.out.print(str1==str2);<br />
地球上有点Java基础的人都知道会输出false，因为==比较的是引用，equals比较的是内容。不是我忽悠大家，你们可以在自己的机子上运行一下，结果是true！原因很简单，String对象被放进常量池里了，再次出现&#8220;java&#8221;字符串的时候，JVM很兴奋地把str2的引用也指向了&#8220;java&#8221;对象，它认为自己节省了内存开销。不难理解吧 呵呵<br />
例子B：<br />
&nbsp;&nbsp; &nbsp;String str1 = new String("java");<br />
&nbsp;&nbsp; &nbsp;String str2 = new String("java");<br />
&nbsp;&nbsp; &nbsp;System.out.print(str1==str2);<br />
看过上例的都学聪明了，这次肯定会输出true！很不幸，JVM并没有这么做，结果是false。原因很简单，例子A中那种声明的方式确实是在String常量池创建&#8220;java&#8221;对象，但是一旦看到new关键字，JVM会在堆中为String分配空间。两者声明方式貌合神离，这也是我把&#8220;如何创建字符串对象&#8221;放到后面来讲的原因。大家要沉住气，还有一个例子。<br />
例子C：<br />
&nbsp;&nbsp; &nbsp;String str1 = "java";<br />
&nbsp;&nbsp; &nbsp;String str2 = "blog";<br />
&nbsp;&nbsp; &nbsp;String s = str1+str2;<br />
&nbsp;&nbsp; &nbsp;System.out.print(s=="javablog");<br />
再看这个例子，很多同志不敢妄言是true还是false了吧。爱玩脑筋急转弯的人会说是false吧&#8230;&#8230;恭喜你，你会抢答了！把那个&#8220;吧&#8221;字去掉你就完全正确。原因很简单，JVM确实会对型如String str1 = "java"; 的String对象放在字符串常量池里，但是它是在编译时刻那么做的，而String s = str1+str2; 是在运行时刻才能知道（我们当然一眼就看穿了，可是Java必须在运行时才知道的，人脑和电脑的结构不同），也就是说str1+str2是在堆里创建的，s引用当然不可能指向字符串常量池里的对象。没崩溃的人继续看例子D。<br />
例子D：<br />
&nbsp;&nbsp; &nbsp;String s1 = "java";<br />
&nbsp;&nbsp; &nbsp;String s2 = new String("java");<br />
&nbsp;&nbsp; &nbsp;System.out.print(s1.intern()==s2.intern());<br />
intern()是什么东东？反正结果是true。如果没用过这个方法，而且训练有素的程序员会去看JDK文档了。简单点说就是用intern()方法就可以用&#8220;==&#8221;比较字符串的内容了。在我看到intern()方法到底有什么用之前，我认为它太多余了。其实我写的这一条也很多余，intern()方法还存在诸多的问题，如效率、实现上的不统一&#8230;&#8230;<br />
例子E：<br />
&nbsp;&nbsp; &nbsp;String str1 = "java";<br />
&nbsp;&nbsp; &nbsp;String str2 = new String("java");<br />
&nbsp;&nbsp; &nbsp;System.out.print(str1.equals(str2));<br />
无论在常量池还是堆中的对象，用equals()方法比较的就是内容，就这么简单！看完此条的人一定很后悔，但是在开始我劝你别看了&#8230;&#8230;<br />
<br />
后记：用彪哥的话说&#8220;有意思吗？&#8221;，确实没劲。在写这段的时候我也是思量再三，感觉自己像孔乙己炫耀&#8220;茴&#8221;字有几种写法。我查了一下茴 ，回，囘，囬，还有一种是&#8220;口&#8221;字里面有个&#8220;目&#8221;字，后面这四个都加上草字头&#8230;&#8230;<br />
<img src ="http://www.blogjava.net/EricGu/aggbug/284780.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/EricGu/" target="_blank">Eric Gu</a> 2009-06-30 15:34 <a href="http://www.blogjava.net/EricGu/archive/2009/06/30/284780.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>J2SE5中的最新注释功能SuppressWarnings </title><link>http://www.blogjava.net/EricGu/archive/2009/06/16/282626.html</link><dc:creator>Eric Gu</dc:creator><author>Eric Gu</author><pubDate>Tue, 16 Jun 2009 08:02:00 GMT</pubDate><guid>http://www.blogjava.net/EricGu/archive/2009/06/16/282626.html</guid><wfw:comment>http://www.blogjava.net/EricGu/comments/282626.html</wfw:comment><comments>http://www.blogjava.net/EricGu/archive/2009/06/16/282626.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/EricGu/comments/commentRss/282626.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/EricGu/services/trackbacks/282626.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 一、什么是注释                         &nbsp;&nbsp;&nbsp; 说起注释，得先提一提什么是元数据(metadata)。所谓元数据就是数据的数据。也就是说，元数据是描述数据的。就象数据表中的字段一样，每个字段描述了这个字段下的数据的含义。而J2SE5.0中提供的注释就是java源代码的元数据，也就是说注释是描述java源代码的。在J2SE5.0中可以自定...&nbsp;&nbsp;<a href='http://www.blogjava.net/EricGu/archive/2009/06/16/282626.html'>阅读全文</a><img src ="http://www.blogjava.net/EricGu/aggbug/282626.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/EricGu/" target="_blank">Eric Gu</a> 2009-06-16 16:02 <a href="http://www.blogjava.net/EricGu/archive/2009/06/16/282626.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>IntelliJ IDEA使用技巧一览表</title><link>http://www.blogjava.net/EricGu/archive/2009/03/26/262019.html</link><dc:creator>Eric Gu</dc:creator><author>Eric Gu</author><pubDate>Thu, 26 Mar 2009 01:33:00 GMT</pubDate><guid>http://www.blogjava.net/EricGu/archive/2009/03/26/262019.html</guid><wfw:comment>http://www.blogjava.net/EricGu/comments/262019.html</wfw:comment><comments>http://www.blogjava.net/EricGu/archive/2009/03/26/262019.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/EricGu/comments/commentRss/262019.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/EricGu/services/trackbacks/262019.html</trackback:ping><description><![CDATA[<div align="left"><font face="宋体">在使用</font><font face="arial">InelliJ IDEA</font><font face="宋体">的过程中，通过查找资料以及一些自己的摸索，发现这个众多</font><font face="arial">Java</font><font face="宋体">程序员喜欢的</font><font face="arial">IDE</font><font face="宋体">里有许多值得一提的小窍门，如果能熟练的将它们应用于实际开发过程中，相信它会大大节省你的开发时间，而且随之而来的还会有那么一点点成就感：）</font><font face="arial">Try it</font><font face="宋体">！</font></div>
<div align="left"><font face="arial">1</font><font face="宋体">、写代码时用</font><strong><font face="arial">Alt-Insert</font></strong><font face="宋体">（</font><strong><font face="arial">Code|Generate&#8230;</font></strong><font face="宋体">）可以创建类里面任何字段的</font><font face="arial">getter</font><font face="宋体">与</font><font face="arial">setter</font><font face="宋体">方法。</font></div>
<div align="left"><font face="arial"></font></div>
<div align="left"><font face="arial">2</font><font face="宋体">、右键点击断点标记（在文本的左边栏里）激活速查菜单，你可以快速设置</font><strong><font face="arial">enable/disable</font></strong><font face="宋体">断点或者条件它的属性。</font></div>
<div align="left"><img onclick="window.open('http://cnmick.blogchina.com/inc/image001.gif');" src="http://cnmick.blogchina.com/inc/image001.gif" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></div>
<div align="left"><font face="arial">3</font><font face="宋体">、</font><font face="arial">CodeCompletion</font><font face="宋体">（代码完成）属性里的一个特殊的变量是，激活</font><strong><font face="arial">Ctrl-Alt-Space</font></strong><font face="宋体">可以完成在或不在当前文件里的类名。如果类没有引入则</font><font face="arial">import</font><font face="宋体">标志会自动创建。</font></div>
<div align="left"><img onclick="window.open('http://cnmick.blogchina.com/inc/image002.gif');" src="http://cnmick.blogchina.com/inc/image002.gif" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></div>
<div align="left"><font face="arial">4</font><font face="宋体">、使用</font><strong><font face="arial">Ctrl-Shift-V</font></strong><font face="宋体">快捷键可以将最近使用的剪贴板内容选择插入到文本。使用时系统会弹出一个含有剪贴内容的对话框，从中你可以选择你要粘贴的部分。</font></div>
<div align="left"><font face="arial">5</font><font face="宋体">、利用</font><font face="arial">CodeCompletion</font><font face="宋体">（代码完成）属性可以快速地在代码中完成各种不同地语句，方法是先键入一个类名地前几个字母然后再用</font><strong><font face="arial">Ctrl-Space</font></strong><font face="宋体">完成全称。如果有多个选项，它们会列在速查列表里。</font></div>
<div align="left"><font face="arial">&nbsp;<img onclick="window.open('http://cnmick.blogchina.com/inc/image003.gif');" src="http://cnmick.blogchina.com/inc/image003.gif" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></font></div>
<div align="left"><font face="arial">6</font><font face="宋体">、用</font><strong><font face="arial">Ctrl-/</font></strong><font face="宋体">与</font><strong><font face="arial">Ctrl-Shift-/</font></strong><font face="宋体">来注释</font><font face="arial">/</font><font face="宋体">反注释代码行与代码块。</font></div>
<div align="left"><font face="arial">-/</font><font face="宋体">用单行注释标记（&#8220;</font><strong><font face="arial">//&#8230;</font></strong><font face="宋体">&#8221;）来注释</font><font face="arial">/</font><font face="宋体">反注释当前行或者选择地代码块。而</font><font face="arial">Ctrl-Shift-/</font><font face="宋体">则可以用块注释标记（&#8220;</font><strong><font face="arial">/<span style="display: none"> V;}HGn </span>&nbsp;<br />
&#8230;<span style="display: none"> dy/?S GW </span>&nbsp;<br />
/</font></strong><font face="宋体">&#8221;）把所选块包围起来。要反注释一个代码块就在块中任何一个地方按</font><font face="arial">Ctrl-Shift-/</font><font face="宋体">即可。</font></div>
<div align="left"><font face="arial">7</font><font face="宋体">、按</font><strong><font face="arial">Alt-Q</font></strong><font face="宋体">（</font><strong><font face="arial">View|Context Info</font></strong><font face="宋体">）可以不需要移动代码就能查看当前方法地声明。连续按两次会显示当前所编辑的类名。</font></div>
<div align="left"><font face="arial">8</font><font face="宋体">、使用</font><strong><font face="arial">Refactor|Copy Class&#8230;</font></strong><font face="宋体">可以创建一个所选择的类的&#8220;副本&#8221;。这一点很有用，比如，在你想要创建一个大部分内容都和已存在类相同的类时。</font></div>
<div align="left"><font face="arial">9</font><font face="宋体">、在编辑器里</font><strong><font face="arial">Ctrl-D</font></strong><font face="宋体">可以复制选择的块或者没有所选块是的当前行。</font></div>
<div align="left"><font face="arial">10</font><font face="宋体">、</font><strong><font face="arial">Ctrl-W</font></strong><font face="宋体">（选择字）在编辑器里的功能是先选择脱字符处的单词，然后选择源代码的扩展区域。举例来说，先选择一个方法名，然后是调用这个方法的表达式，然后是整个语句，然后包容块，等等。</font></div>
<div align="left"><font face="arial">11</font><font face="宋体">、如果你不想让指示事件细节的&#8220;亮球&#8221;图标在编辑器上显示，通过按</font><strong><font face="arial">Alt-Enter</font></strong><font face="宋体">组合键打开所有事件列表然后用鼠标点击它就可以把这个事件文本附件的亮球置成非活动状态。</font></div>
<div align="left"><font face="宋体">这样以后就不会有指示特殊事件的亮球出现了，但是你仍然可以用</font><strong><font face="arial">Alt-Enter</font></strong><font face="宋体">快捷键使用它。</font></div>
<div align="left"><font face="arial">12</font><font face="宋体">、在使用</font><font face="arial">CodeCompletion</font><font face="宋体">时，可以用逗点（</font><font face="arial">.</font><font face="宋体">）字符，逗号（，）分号（；），空格和其它字符输入弹出列表里的当前高亮部分。选择的名字会随着输入的字符自动输入到编辑器里。</font></div>
<div align="left"><font face="arial">13</font><font face="宋体">、在任何工具窗口里使用</font><strong><font face="arial">Escape</font></strong><font face="宋体">键都可以把焦点移到编辑器上。</font></div>
<div align="left"><strong><font face="arial">Shift-Escape</font></strong><font face="宋体">不仅可以把焦点移到编辑器上而且还可以隐藏当前（或最后活动的）工具窗口。</font></div>
<div align="left"><font face="arial">F12</font><font face="宋体">键把焦点从编辑器移到最近使用的工具窗口。</font></div>
<div align="left"><font face="arial">14</font><font face="宋体">、在调试程序时查看任何表达式值的一个容易的方法就是在编辑器中选择文本（可以按几次</font><strong><font face="arial">Ctrl-W</font></strong><font face="宋体">组合键更有效地执行这个操作）然后按</font><font face="arial">Alt-F8</font><font face="宋体">。</font></div>
<div align="left"><font face="arial">15</font><font face="宋体">、要打开编辑器脱字符处使用的类或者方法</font><font face="arial">Java</font><font face="宋体">文档的浏览器，就按</font><strong><font face="arial">Shift-F1</font></strong><font face="宋体">（右键菜单的</font><strong><font face="arial">External JavaDoc</font></strong><font face="宋体">）。</font></div>
<div align="left"><font face="宋体">要使用这个功能须要把加入浏览器的路径，在&#8220;</font><strong><font face="arial">General</font></strong><font face="宋体">&#8221;选项中设置（</font><strong><font face="arial">Options | IDE Settings</font></strong><font face="宋体">），另外还要把创建的</font><font face="arial">Java</font><font face="宋体">文档加入到工程中（</font><strong><font face="arial">File | Project Properties</font></strong><font face="宋体">）。</font></div>
<div align="left"><font face="arial">16</font><font face="宋体">、用</font><strong><font face="arial">Ctrl-F12</font></strong><font face="宋体">（</font><strong><font face="arial">View | File Structure Popup</font></strong><font face="宋体">）键你可以在当前编辑的文件中快速导航。</font></div>
<div align="left"><font face="宋体">这时它会显示当前类的成员列表。选中一个要导航的元素然后按</font><font face="arial">Enter</font><font face="宋体">键或</font><font face="arial">F4</font><font face="宋体">键。要轻松地定位到列表中的一个条目，只需键入它的名字即可。</font></div>
<div align="left"><font face="arial">17</font><font face="宋体">、在代码中把光标置于标记符或者它的检查点上再按</font><strong><font face="arial">Alt-F7</font></strong><font face="宋体">（右键菜单中的</font><strong><font face="arial">Find Usages&#8230;</font></strong><font face="宋体">）会很快地查找到在整个工程中使用地某一个类、方法或者变量的位置。</font></div>
<div align="left"><font face="arial">18</font><font face="宋体">、按</font><strong><font face="arial">Ctrl-N</font></strong><font face="宋体">（</font><strong><font face="arial">Go to | Class&#8230;</font></strong><font face="宋体">）再键入类的名字可以快速地在编辑器里打开任何一个类。从显示出来的下拉列表里选择类。</font></div>
<div align="left"><font face="arial"><img onclick="window.open('http://cnmick.blogchina.com/inc/image004.gif');" src="http://cnmick.blogchina.com/inc/image004.gif" width="300" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></font></div>
<div align="left"><font face="宋体">同样的方法你可以通过使用</font><strong><font face="arial">Ctrl-Shift-N</font></strong><font face="宋体">（</font><strong><font face="arial">Go to | File&#8230;</font></strong><font face="宋体">）打开工程中的非</font><font face="arial">Java</font><font face="宋体">文件。</font></div>
<div align="left"><font face="arial">19</font><font face="宋体">、要导航代码中一些地方使用到的类、方法或者变量的声明，把光标放在查看项上再按</font><font face="arial">Ctrl-B</font><font face="宋体">即可。也可以通过按</font><font face="arial">Ctrl</font><font face="宋体">键的同时在查看点上单击鼠标键调转到声明处。</font></div>
<div align="left"><font face="arial"><img onclick="window.open('http://cnmick.blogchina.com/inc/image005.gif');" src="http://cnmick.blogchina.com/inc/image005.gif" width="300" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></font></div>
<div align="left"><font face="arial">20</font><font face="宋体">、把光标放到查看点上再按</font><strong><font face="arial">Ctrl-Alt-B</font></strong><font face="宋体">可以导航到一个抽象方法的实现代码。</font></div>
<div align="left"><font face="arial">21</font><font face="宋体">、要看一个所选择的类的继承层次，按</font><strong><font face="arial">Ctrl-H</font></strong><font face="宋体">（</font><strong><font face="arial">Browse Type Hierarchy</font></strong><font face="宋体">）即可。也可以激活编辑器中的继承关系视图查看当前编辑类的继承关系。</font></div>
<div align="left"><font face="arial"></font></div>
<div align="left">　<img onclick="window.open('http://cnmick.blogchina.com/inc/image006.gif');" src="http://cnmick.blogchina.com/inc/image006.gif" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></div>
<div align="left"><font face="arial">22</font><font face="宋体">、使用</font><strong><font face="arial">Ctrl-Shift-F7</font></strong><font face="宋体">（</font><strong><font face="arial">Search | Highlight Usages in File</font></strong><font face="宋体">）可以快速高亮显示当前文件中某一变量的使用地方。按</font><font face="arial">Escape</font><font face="宋体">清除高亮显示。</font></div>
<div align="left"><font face="arial">23</font><font face="宋体">、用</font><strong><font face="arial">Alt-F3</font></strong><font face="宋体">（</font><strong><font face="arial">Search | Incremental Search</font></strong><font face="宋体">）在编辑器中实现快速查查找功能。</font></div>
<div align="left"><font face="宋体">在&#8220;</font><strong><font face="arial">Search for:</font></strong><font face="宋体">&#8221;提示工具里输入字符，使用箭头键朝前和朝后搜索。按</font><strong><font face="arial">Escape</font></strong><font face="宋体">退出。</font></div>
<div align="left"><font face="arial">24</font><font face="宋体">、按</font><strong><font face="arial">Ctrl-J</font></strong><font face="宋体">组合键来执行一些你记不起来的</font><strong><font face="arial">Live Template</font></strong><font face="宋体">缩写。比如，键&#8220;</font><strong><font face="arial">it</font></strong><font face="宋体">&#8221;然后按</font><strong><font face="arial">Ctrl-J</font></strong><font face="宋体">看看有什么发生。</font></div>
<div align="left"><font face="arial"><img onclick="window.open('http://cnmick.blogchina.com/inc/image007.gif');" src="http://cnmick.blogchina.com/inc/image007.gif" width="300" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></font></div>
<div align="left"><font face="arial">25</font><font face="宋体">、</font><strong><font face="arial">Introduce Variable</font></strong><font face="宋体">整合帮助你简化代码中复杂的声明。举个例子，在下面的代码片断里，在代码中选择一个表达式：</font></div>
<div align="left"><font face="arial"><img onclick="window.open('http://cnmick.blogchina.com/inc/image008.gif');" src="http://cnmick.blogchina.com/inc/image008.gif" width="300" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></font></div>
<div align="left"><font face="arial"></font><font face="宋体">然后按</font><strong><font face="arial">Ctrl-Alt-V</font></strong><font face="宋体">（</font><strong><font face="arial">Refactor | Introduce Variable</font></strong><font face="宋体">）就会出现下面的结果：</font></div>
<div align="left"><font face="arial"><img onclick="window.open('http://cnmick.blogchina.com/inc/image009.gif');" src="http://cnmick.blogchina.com/inc/image009.gif" width="300" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></font></div>
<div align="left"><font face="arial">26</font><font face="宋体">、</font><strong><font face="arial">Ctrl-Shift-J</font></strong><font face="宋体">快捷键把两行合成一行并把不必要的空格去掉以匹配你的代码格式。</font></div>
<div align="left"><font face="arial">27</font><font face="宋体">、</font><strong><font face="arial">Ctrl-Shift-Backspace</font></strong><font face="宋体">（</font><strong><font face="arial">Go to | Last Edit Location</font></strong><font face="宋体">）让你调转到代码中所做改变的最后一个地方。</font></div>
<div align="left"><font face="宋体">多按几次</font><strong><font face="arial">Ctrl-Shift-Backspace</font></strong><font face="宋体">查看更深的修改历史。</font></div>
<div align="left"><font face="arial">28</font><font face="宋体">、用</font><strong><font face="arial">Tools | Reformat Code&#8230;</font></strong><font face="宋体">根据你的代码样式参考（查看</font><strong><font face="arial">Options | IDE Setting | Code Style</font></strong><font face="宋体">）格式化代码。</font></div>
<div align="left"><font face="宋体">使用</font><strong><font face="arial">Tools | Optimize Imports&#8230;</font></strong><font face="宋体">可以根据设置（查看</font><strong><font face="arial">Options | IDE Setting | Code Style | Imports</font></strong><font face="宋体">）自动&#8220;优化&#8221;</font><strong><font face="arial">imports</font></strong><font face="宋体">（清除无用的</font><strong><font face="arial">imports</font></strong><font face="宋体">等）。</font></div>
<div align="left"><font face="arial">29</font><font face="宋体">、使用</font><font face="arial">IDEA</font><font face="宋体">的</font><strong><font face="arial">Live Templates | Live Templates</font></strong><font face="宋体">让你在眨眼间创建许多典型代码。比如，在一个方法里键入</font></div>
<div align="left"><font face="arial"><img onclick="window.open('http://cnmick.blogchina.com/inc/image010.gif');" src="http://cnmick.blogchina.com/inc/image010.gif" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></font></div>
<div align="left"><font face="宋体">再按</font><strong><font face="arial">Tab</font></strong><font face="宋体">键看有什么事情发生了。</font></div>
<div align="left"><font face="arial"><img onclick="window.open('http://cnmick.blogchina.com/inc/image011.gif');" src="http://cnmick.blogchina.com/inc/image011.gif" width="300" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></font></div>
<div align="left"><font face="宋体">用</font><font face="arial">Tab</font><font face="宋体">键在不同的模板域内移动。查看</font><strong><font face="arial">Options | Live Templates</font></strong><font face="宋体">获取更多的细节。</font></div>
<div align="left"><font face="arial">30</font><font face="宋体">、要查看一个文件中修改的本地历史，激活右键菜单里的</font><strong><font face="arial">Local VCS | Show History&#8230;</font></strong><font face="宋体">。也许你可以导航不同的文件版本，看看它们的不同之处再回滚到以前的任何一个版本吧。</font></div>
<div align="left"><font face="宋体">使用同样的右键菜单条目还可以看到一个目录里修改的历史。有了这个特性你就不会丢失任何代码了。</font></div>
<div align="left"><font face="arial">31</font><font face="宋体">、如果要了解主菜单里每一个条目的用途，把鼠标指针移到菜单条目上再应用程序框架的底部的状态栏里就会显示它们的一些简短描述，也许会对你有帮助。</font></div>
<div align="left"><font face="arial">32</font><font face="宋体">、要在编辑器里显示方法间的分隔线，打开</font><strong><font face="arial">Options | IDE Settings | Editor</font></strong><font face="宋体">，选中&#8220;</font><strong><font face="arial">Show method separators</font></strong><font face="宋体">&#8221;检查盒（</font><strong><font face="arial">checkbox</font></strong><font face="宋体">）。</font></div>
<div align="left"><font face="arial">33</font><font face="宋体">、<strong>用</strong></font><strong><font face="arial">Alt-Up</font></strong><font face="宋体">和</font><strong><font face="arial">Alt-Down</font></strong><font face="宋体">键可以在编辑器里不同的方法之间快速移动。</font></div>
<div align="left"><font face="arial">34</font><font face="宋体">、用</font><strong><font face="arial">F2/Shift-F2</font></strong><font face="宋体">键在高亮显示的语法错误间跳转。</font></div>
<div align="left"><font face="宋体">用</font><strong><font face="arial">Ctrl-Alt-Down/Ctrl-Alt-Up</font></strong><font face="宋体">快捷键则可以在编译器错误信息或者查找操作结果间跳转。</font></div>
<div align="left"><font face="arial">35</font><font face="宋体">、通过按</font><strong><font face="arial">Ctrl-O</font></strong><font face="宋体">（</font><strong><font face="arial">Code | Override Methods&#8230;</font></strong><font face="宋体">）可以很容易地重载基本类地方法。</font></div>
<div align="left"><font face="宋体">要完成当前类</font><strong><font face="arial">implements</font></strong><font face="宋体">的（或者抽象基本类的）接口的方法，就使用</font><strong><font face="arial">Ctrl-I</font></strong><font face="宋体">（</font><strong><font face="arial">Code | Implement Methods&#8230;</font></strong><font face="宋体">）。</font></div>
<div align="left"><font face="arial">36</font><font face="宋体">、如果光标置于一个方法调用的括号间，按</font><strong><font face="arial">Ctrl-P</font></strong><font face="宋体">会显示一个可用参数的列表。</font></div>
<div align="left"><font face="arial"><img onclick="window.open('http://cnmick.blogchina.com/inc/image012.gif');" src="http://cnmick.blogchina.com/inc/image012.gif" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></font></div>
<div align="left"><font face="arial">37</font><font face="宋体">、要快速查看编辑器脱字符处使用的类或方法的</font><font face="arial">Java</font><font face="宋体">文档，按</font><strong><font face="arial">Ctrl-Q</font></strong><font face="宋体">（在弹出菜单的</font><strong><font face="arial">Show Quick JavaDoc</font></strong><font face="宋体">里）即可。</font></div>
<div align="left"><font face="arial"><img onclick="window.open('http://cnmick.blogchina.com/inc/image013.gif');" src="http://cnmick.blogchina.com/inc/image013.gif" width="300" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></font></div>
<div align="left"><font face="arial">38</font><font face="宋体">、像</font><strong><font face="arial">Ctrl-Q</font></strong><font face="宋体">（</font><strong><font face="arial">Show Quick JavaDoc</font></strong><font face="宋体">显示简洁</font><font face="arial">Java</font><font face="宋体">文档），</font><strong><font face="arial">Ctrl-P</font></strong><font face="宋体">（</font><strong><font face="arial">Show Parameter Info</font></strong><font face="宋体">显示参数信息），</font><strong><font face="arial">Ctrl-B</font></strong><font face="宋体">（</font><strong><font face="arial">Go to Declaration</font></strong><font face="宋体">跳转到声明），</font><strong><font face="arial">Shift-F1</font></strong><font face="宋体">（</font><strong><font face="arial">External JavaDoc</font></strong><font face="宋体">外部</font><font face="arial">Java</font><font face="宋体">文档）以及其它一些快捷键不仅可以在编辑器里使用，也可以应用在代码完成右键列表里。</font></div>
<div align="left"><font face="arial">39</font><font face="宋体">、</font><strong><font face="arial">Ctrl-E</font></strong><font face="宋体">（</font><strong><font face="arial">View | Recent Files</font></strong><font face="宋体">）弹出最近访问的文件右键列表。选中文件按</font><strong><font face="arial">Enter</font></strong><font face="宋体">键打开。</font></div>
<div align="left"><font face="arial">40</font><font face="宋体">、在</font><font face="arial">IDEA</font><font face="宋体">中可以很容易地对你的类，方法以及变量进行重命名并在所有使用到它们的地方自动更正。</font></div>
<div align="left"><font face="宋体">试一下，把编辑器脱字符置于任何一个变量名字上然后按</font><strong><font face="arial">Shift-F6</font></strong><font face="宋体">（</font><strong><font face="arial">Refactor | Rename&#8230;</font></strong><font face="宋体">）。在对话框里键入要显示地新名字再按</font><font face="arial">Enter</font><font face="宋体">。你会浏览到使用这个变量地所有地方然后按&#8220;</font><strong><font face="arial">Do Refactor</font></strong><font face="宋体">&#8221;按钮结束重命名操作。</font></div>
<div align="left"><font face="arial">41</font><font face="宋体">、要在任何视图（</font><strong><font face="arial">Project View</font></strong><font face="宋体">工程视图，</font><strong><font face="arial">Structure View</font></strong><font face="宋体">结构视图或者其它视图）里快速</font></div>
<div align="left"><font face="宋体">选择当前编辑地部分（类，文件，方法或者字段），按</font><strong><font face="arial">Alt-F1</font></strong><font face="宋体">（</font><strong><font face="arial">View | Select in&#8230;</font></strong><font face="宋体">）。</font></div>
<div align="left"><font face="arial"><img onclick="window.open('http://cnmick.blogchina.com/inc/image014.gif');" src="http://cnmick.blogchina.com/inc/image014.gif" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></font></div>
<div align="left"><font face="arial">42</font><font face="宋体">、在&#8220;</font><strong><font face="arial">new</font></strong><font face="宋体">&#8221;字符后实例化一个已知类型对象时也许你会用到</font><font face="arial">SmartType</font><font face="宋体">代码完成这个特性。比如，键入</font></div>
<div align="left"><font face="arial">&nbsp;<img onclick="window.open('http://cnmick.blogchina.com/inc/image015.gif');" src="http://cnmick.blogchina.com/inc/image015.gif" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></font></div>
<div align="left"><font face="宋体">再按</font><strong><font face="arial">Ctrl-Shift-Space</font></strong><font face="宋体">：</font></div>
<div align="left"><font face="arial"><img onclick="window.open('http://cnmick.blogchina.com/inc/image016.gif');" src="http://cnmick.blogchina.com/inc/image016.gif" width="300" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></font></div>
<div align="left"><font face="arial">43</font><font face="宋体">、通过使用</font><font face="arial">SmartType</font><font face="宋体">代码完成，在</font><font face="arial">IDEA</font><font face="宋体">中创建接口的整个匿名</font><strong><font face="arial">implementation</font></strong><font face="宋体">也是非常容易的，比如，对于一些</font><font face="arial">listener</font><font face="宋体">（监听器），可以键入</font></div>
<div align="left"><font face="arial">&nbsp; Component component;</font></div>
<div align="left"><font face="arial">&nbsp; component.addMouseListener(</font></div>
<div align="left"><font face="arial">&nbsp;&nbsp;&nbsp; new w w <strong><em><font color="#005555">&lt;caret is here&gt;</font></em></strong>&nbsp;</font></div>
<div align="left"><font face="arial">&nbsp; );</font></div>
<div align="left"><font face="宋体">然后再按</font><strong><font face="arial">Ctrl-Shift-Space</font></strong><font face="宋体">看看有什么发生了。</font></div>
<div align="left"><font face="arial">44</font><font face="宋体">、在你需要设置一个已知类型的表达式的值时用</font><font face="arial">SmartType</font><font face="宋体">代码完成也很有帮助。比如，键入</font></div>
<div align="left"><font face="arial">String s = (<strong><em><font color="#005555">&lt;caret is here&gt;</font></em></strong>&nbsp;</font></div>
<div align="left"><font face="宋体">再按</font><strong><font face="arial">Ctrl-Shift-Space</font></strong><font face="宋体">看看会有什么出现。</font></div>
<div align="left"><font face="arial">45</font><font face="宋体">、在所有视图里都提供了速查功能：在树里只需键入字符就可以快速定位到一个条目。</font></div>
<div align="left"><font face="arial">46</font><font face="宋体">、当你想用代码片断捕捉异常时，在编辑器里选中这个片断，按</font><strong><font face="arial">Ctrl-Alt-T</font></strong><font face="宋体">（</font><strong><font face="arial">Code | Surround with&#8230;</font></strong><font face="宋体">）然后选择</font><font face="arial">&#8220;<strong>try/catch</strong>&#8221;</font><font face="宋体">。它会自动产生代码片断中抛出的所有异常的捕捉块。在</font><strong><font face="arial">Options | File Templates | Code tab</font></strong><font face="宋体">中你还可以自己定制产生捕捉块的模板。</font></div>
<div align="left"><font face="宋体">用列表中的其它项可以包围别的一些结构。</font></div>
<div align="left"><font face="arial"><img onclick="window.open('http://cnmick.blogchina.com/inc/image017.gif');" height="200" src="http://cnmick.blogchina.com/inc/image017.gif" width="300" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></font></div>
<div align="left"><font face="arial">47</font><font face="宋体">、在使用代码完成时，用</font><font face="arial">Tab</font><font face="宋体">键可以输入弹出列表里的高亮显示部分。</font></div>
<div align="left"><font face="宋体">不像用</font><strong><font face="arial">Enter</font></strong><font face="宋体">键接受输入，这个选中的名字会覆盖掉脱字符右边名字的其它部分。这一点在用一个方法或者变量名替换另一个时特别有用。</font></div>
<div align="left"><font face="arial">48</font><font face="宋体">、在声明一个变量时代码完成特性会给你显示一个建议名。比如，开始键入</font><font face="arial">&#8220;private FileOutputStream&#8221;</font><font face="宋体">然后按</font><strong><font face="arial">Ctrl-Space</font></strong></div>
<div align="left"><font face="arial"><img onclick="window.open('http://cnmick.blogchina.com/inc/image018.gif');" src="http://cnmick.blogchina.com/inc/image018.gif" width="300" onload="if(this.width  alt="" />'300')this.width='300';if(this.height>'200')this.height='200';" border=0></font></div>
<div align="left"><font face="宋体">在</font><strong><font face="arial">Options | IDE Setting | Code Style</font></strong><font face="宋体">中还可以为本地变量，参数，实例及静态字段定制名字。<br />
<br />
</font></div>
 <img src ="http://www.blogjava.net/EricGu/aggbug/262019.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/EricGu/" target="_blank">Eric Gu</a> 2009-03-26 09:33 <a href="http://www.blogjava.net/EricGu/archive/2009/03/26/262019.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>