JDK5.0的11个主要新特征[9]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

int intPrimitive;

ArrayList arrayList = new ArrayList();

intPrimitive = 11;

intObject = new Integer(intPrimitive);

arrayList.put(intObject); // 不能放入int类型,只能使Integer

新的实现方式

int intPrimitive;

ArrayList arrayList = new ArrayList();

intPrimitive = 11;

//在这里intPrimitive被自动的转换为Integer类型

arrayList.put(intPrimitive);

5           静态导入(Static Imports)

很简单的东西,看一个例子:

没有静态导入

Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));

有了静态导入

import static java.lang.Math.*;

sqrt(pow(x, 2) + pow(y, 2));

 

其中import static java.lang.Math.*;就是静态导入的语法,它的意思是导入Math类中的所有static方法和属性。这样我们在使用这些方法和属性时就不必写类名。

需要注意的是默认包无法用静态导入,另外如果导入的类中有重复的方法和属性则需要写出类名,否则编译时无法通过。

6          枚举类(Enumeration Classes)

用法:public enum Name {types, ….}

简单的例子:

public enum Colors {Red, Yellow, Blue, Orange, Green, Purple, Brown, Black}

public static void main(String[] args){

本文关键:JDK5.0的11个主要新特征
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top