JAVA集合泛型,类型擦除,类型通配符上限之类的知识点

感觉定义要比PYTHON严谨很多,一切源于静态语言的特点吧。。

于是语法上就复杂很多,值不值得呢?

参考测试URL:

http://www.cnblogs.com/lwbqqyumidi/p/3837629.html

 

JAVA集合泛型,类型擦除,类型通配符上限之类的知识点
import java.util.*;

public class TestCollection {
  public static void main(String args[]) {
    List<String> list = new ArrayList<String>();
    list.add("QQYmydmidi");
    list.add("corn");
    //list.add(100);

    for (int i=0; i < list.size(); i++) {
      String name = (String) list.get(i);
      System.out.println("name: " + name);
    }

    Box<String> name = new Box<String>("coooorn");
    Box<Integer> age = new Box<Integer>(712);
    Box<Number> number = new Box<Number>(314);
    System.out.println("name: " + name.getData());
    System.out.println("age: " + age.getData());

    System.out.println("name class: " + name.getClass());
    System.out.println("age class: " + age.getClass());
    System.out.println(name.getClass() == age.getClass());

    getData(name);
    getData(age);
    getData(number);

    //getUpperNumberData(name);
    getUpperNumberData(age);
    getUpperNumberData(number);
  }
  public static void getData(Box<?> data) {
    System.out.println("data: " + data.getData());
  }
  public static void getUpperNumberData(Box<? extends Number> data) {
    System.out.println("data: " + data.getData());
  }
}

class Box<T> {
  private T data;
  public Box(){};

  public Box(T data) {
    this.data = data;
  }
  public T getData() {
    return data;
  }
}
JAVA集合泛型,类型擦除,类型通配符上限之类的知识点

JAVA集合泛型,类型擦除,类型通配符上限之类的知识点

上一篇:java泛型 通配符详解及实践


下一篇:阿里云服务器ECS免费使用体验