CopyRight owned by the original author.--(www.MegaEntry.com)
1、使工程基于JDK 5.0 虽说JBuilder 2005支持JDK 5.0,但其默认的JDK版本是1.4.2,要使工程的JDK版本为5.0,你必须安装JDK 5.0,并在JBuilder下进行相应的设置。关于JDK 5.0的详细设置我们不作过多的描述,简而言之,它主要包括以下的步骤: 1) 安装JDK 5.0(从http://java.sun.com/j2se/1.5.0/download.jsp下载)。 2) Tools->Configure->JDKs...通用指定JDK5.0的安装路径设置JDK。 3) Project->Project Properties...->在Paths设置页,将JDK设置为JDK 5.0。 4) Project->Project Properties...->Build->在Java设置页,将Language features:设置为Java 2 SDK,V 5.0(generics enabled),将Target VM设置为Java 2 SDK,v 5.0 and later。 只有将工程的JDK版本设置为JDK 5.0,才可以进行有关JDK 5.0的代码重构。 2、优化循环CopyRight owned by the original author.--(www.MegaEntry.com)
JDK 5.0引入了更高效的循环,称之为JDK 5.0样式的循环,包括: ・数组遍历 ・List遍历 ・Iterator的for循环 ・Iterator的while循环 JBuilder提供了将低版本JDK对应的循环代码转换这JDK5.0循环风格的重构方法,我们通过一个数组遍历的重构对此做说明,请看下面的低版本JDK循环代码: 代码清单 11 低版本循环样式| 1. public static void arrayLoopRefactoring() CopyRight owned by the original author.--(www.MegaEntry.com) 2. {3. int[] myArray = {1 , 2 , 3 , 4} ;4. for(int x = 0 ; x < myArray.length ; x++) {5. System.out.println(myArray[x]) ;6. }7. } |
图 22 循环重构对话框 在Loop variable name中为数组循环临时变量指定一个变量名,这里我们设置为item,按OK完成重构,JBuilder生成JDK 5.0风格循环代码,如下所示: 代码清单 12 JDK 5.0样式循环
| 1. public static void arrayLoopRefactoring() CopyRight owned by the original author.--(www.MegaEntry.com) 2. {3. int[] myArray = {1 , 2 , 3 , 4} ;4. for(int item : myArray) {5. System.out.println(item) ;6. }7. } |
| 1. package myrefactor ;2. public class Jdk53. {4. … CopyRight owned by the original author.--(www.MegaEntry.com) 5. public static void autoBoxingPreliminary(Integer intObject)6. {7. System.out.println(intObject) ;8. }9. 10. public static void autoBoxingRefactoring()11. {12. autoBoxingPreliminary(new Integer(8)) ;13. }14. …15. } |
| 1. package myrefactor ; CopyRight owned by the original author.--(www.MegaEntry.com) 2. public class Jdk53. {4. …5. public static void autoBoxingPreliminary(Integer intObject)6. {7. System.out.println(intObject) ;8. }9. 10. public static void autoBoxingRefactoring()11. {12. autoBoxingPreliminary(8) ;13. }14. …15. } |
CopyRight owned by the original author.--(www.MegaEntry.com)
4、非泛型转泛型 在低版本中,集合中的对象在使用前需要进行显示的类型转换,如String s = (String)iter.next(),JDK 5.0引入了泛型的概念,加入了编译期类型安全检查,取消了强制类型转换,节省了代码,下面是大家熟悉的传统的List操作代码: 代码清单 15 非泛型的代码| 1. public static void genericsArrayList()2. {3. List list = new ArrayList() ;4. list.add(0 , new Integer(23)) ;5. int total = ( (Integer) list.get(0)).intValue() ;6. System.out.println(total) ;7. } |

CopyRight owned by the original author.--(www.MegaEntry.com)
图 23 泛型重构对话框 在Type Argument中指定一个数据类型,JBuilder已经通过分析代码默认了最适合的数据类型,一般情况下无需调整。按OK后完成泛型的代码重构: 代码清单 泛型的代码| 1. public static void genericsArrayList()2. {3. List<Integer> list = new ArrayList<Integer> () ;4. list.add(0 , new Integer(23)) ;5. int total = (list.get(0)).intValue() ;6. System.out.println(total) ;7. } |