Home > Database > Mysql Tutorial > JDK 1.5之Generics_MySQL

JDK 1.5之Generics_MySQL

WBOY
Release: 2016-06-01 14:07:08
Original
1613 people have browsed it

JDK

  Generics 是JDK 1.5 一个最重要的特性,主要用来处理Collection。

  以下代码在JDK 1.5 调试通过。

  代码实例1: Demo.java

  package maoxiang.examples.jdk15.generics;
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.HashMap;
  import java.util.LinkedList;
  import java.util.List;
  import java.util.Map;

  /**
  * @author 毛翔
  *
  * 演示如何使用Generics 特性。代码来自于 Generics 教程:
  * http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
  *
  * Generics类似于C++中的模板。
  * 区别:
  * 1.
  * 2.
  */

  public class Demo {
  public static void main(String[] args) {}

  /**
  * 最简单的用法
  */

  public void Test1() {

  // 以前的用法
  //List myIntList = new LinkedList(); // 1
  //myIntList.add(new Integer(0)); // 2
  //Integer x = (Integer) myIntList.iterator().next(); // 3 需要强制转换
  // 1.5 的用法
  List myIntList = new LinkedList(); // 1’
  myIntList.add(new Integer(0)); //2’
  Integer x = myIntList.iterator().next(); // 3’
  }

  /**
  * 匿名字符的用法
  */

  public void Test2() {
  List list = new ArrayList();
  //通过匿名字符打印一个集合

  Wildcards(list);
  Wildcards1();
  /*
  * 如果 Wildcards2 定义为Wildcards2(List shapes)
  * 以下调用错误
  */
  Wildcards2(list);
  }

  public void Wildcards(Collection c) {
  // 以前的用法
  //Iterator i = c.iterator();
  //for (int k = 0; k   //
  log(i.next());
   /

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template