转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> JAVA开发 >> 正文
JDK5.0的11个主要新特征         ★★★★

JDK5.0的11个主要新特征

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2036 更新时间:2009/4/22 23:33:14

1  泛型(Generic)
  1.1 说明

  增强了java的类型安全,可以在编译期间对容器内的对象进行类型检查,在运行期不必进行类型的转换。而在j2se5之前必须在运行期动态进行容器内对象的检查及转换

  减少含糊的容器,可以定义什么类型的数据放入容器

ArrayList listOfIntegers; // is new to the syntax
Integer integerObject;
listOfIntegers = new ArrayList(); // is new to the syntax
listOfIntegers.add(new Integer(10)); // 只能是Integer类型
integerObject = listOfIntegers.get(0); // 取出对象不需要转换
  1.2   用法

  声明及实例化泛型类:

HashMap hm = new HashMap();
//不能使用原始类型
GenList nList = new GenList();  //编译错误

  J2SE 5.0目前不支持原始类型作为类型参数(type parameter)

  定义泛型接口:

public interface GenInterface {

    void func(T t);
}
  定义泛型类:

public class ArrayList { ... }

public class GenMap { ... }
  例1:

public class MyList extends LinkedList

{
       public void swap(int i, int j)
       {
              Element temp = this.get(i);
              this.set(i, this.get(j));
              this.set(j, temp);
       }

       public static void main(String[] args)
       {
              MyList list = new MyList();
              list.add("hi");
              list.add("andy");
              System.out.println(list.get(0) + " " + list.get(1));
              list.swap(0,1);
              System.out.println(list.get(0) + " " + list.get(1));
       }
}
  例2:

public class GenList {

       private T[] elements;
       private int size = 0;
       private int length = 0;

       public GenList(int size) {
              elements = (T[])new Object[size];
              this.size = size;
       }

       public T get(int i) {
              if (i < length) {
                     return elements[i];
              }
              return null;
       }

       public void add(T e) {
              if (length < size - 1)
                     elements[length++] = e;
       }
}
  泛型方法:

public class TestGenerics{

       public String getString(T obj) { //实现了一个泛型方法
              return obj.toString();
       }

       public static void main(String [] args){
              TestGenerics t = new TestGenerics();
              String s = "Hello";
              Integer i = 100;
              System.out.println(t.getString(s));
              System.out.println(t.getString(i));
              }
}

  1.3 受限泛型

  受限泛型是指类型参数的取值范围是受到限制的. extends关键字不仅仅可以用来声明类的继承关系, 也可以用来声明类型参数(type parameter)的受限关系.例如, 我们只需要一个存放数字的列表, 包括整数(Long, Integer, Short), 实数(Double, Float), 不能用来存放其他类型, 例如字符串(String), 也就是说, 要把类型参数T的取值泛型限制在Number极其子类中.在这种情况下, 我们就可以使用extends关键字把类型参数(type parameter)限制为数字

  示例

public class Limited {

       public static void main(String[] args) {
              Limited number;   //正确
              Limited str;       //编译错误
       }
}
  1.4  泛型与异常

  类型参数在catch块中不允许出现,但是能用在方法的throws之后。例:

import java.io.*;

interface Executor {
       void execute() throws E;
}

public class GenericExceptionTest {
       public static void main(String args[]) {
              try {
                     Executor e = new Executor() {
                            public void execute() throws IOException{
                                   // code here that may throw an
                                   // IOException or a subtype of
                                   // IOException
                            }
                            };
                     e.execute();
              } catch(IOException ioe) {
                     System.out.println("IOException: " + ioe);
                     ioe.printStackTrace();
              }
       }
}
  1.5 泛型的通配符"?"

  "?"可以用来代替任何类型, 例如使用通配符来实现print方法。

public static void print(GenList list) {})


  1.6  泛型的一些局限型

  不能实例化泛型

T t = new T(); //error


  不能实例化泛型类型的数组

T[] ts= new T[10];   //编译错误


  不能实例化泛型参数数

Pair[] table = new Pair(10); // ERROR


  类的静态变量不能声明为类型参数类型

public class GenClass {

     private static T t;    //编译错误
}
  泛型类不能继承自Throwable以及其子类

public GenExpection extends Exception{}    //编译错误


  不能用于基础类型int等

Pair //error

Pair //right
 

2 增强循环(Enhanced for Loop)
  旧的循环

LinkedList list = new LinkedList();

list.add("Hi");
list.add("everyone!");
list.add("Was");
list.add("the");
list.add("pizza");
list.add("good?");
for (int i = 0; i < list.size(); i++)
       System.out.println((String) list.get(i));
//或者用以下循环
//for(Iterator iter = list.iterator(); iter.hasNext(); ) {
//Integer stringObject = (String)iter.next();
// ... more statements to use stringObject...
//}
  新的循环

LinkedList list = new LinkedList();

list.add("Hi");
list.add("everyone!");
list.add("Was");
list.add("the");
list.add("pizza");
list.add("good?");
for (String s : list)
       System.out.println(s);
  很清晰、方便,一看便知其用法

3 可变参数(Variable Arguments)
  实现了更灵活的方法参数传入方式,System.out.printf是个很好的例子

  用法:void test(Object … args)

  一个很容易理解的例子

public static int add(int ... args){

       int total = 0;   
       for (int i = 0; i < args.length; i++)
              total += args[i];     
       return total;
}
public static void main(String[] args){
 &

[1] [2] [3]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台