1.例子用到的類
package testGeneric04;
/**
* 定義一個水果類
* 它有一個子類Apple
* 還有一個準備類 A
* @author Wang
*
*/
public class Fruit {
}
class Apple extends Fruit {
}
class A <T> {
}
2.泛型沒有多态:
package testGeneric04;
/**
* 泛型是沒有多态的
* 總而言之 一句話 泛型沒有多态
* @author Wang
*
*/
public class NoPoly {
public static void main(String[] args) {
A <Fruit> a = new A <Fruit> ();
//A <Fruit> a = new A <Apple> ();在這裡編譯是通不過的 因為泛型沒有多态
}
//形參使用多态
public static void test(A <Fruit> f){
}
//傳回類型使用多态
public static A<Fruit> test2(){
//return new A<Apple>();
return new A <Fruit>();
}
}
3.沒有泛型數組:
package testGeneric04;
import java.util.ArrayList;
/**
* 沒有泛型數組
* 聲明的時候可以使用泛型 但是建立的話就會失敗
* @author Wang
*
*/
public class Array {
public static void main(String[] args) {
Integer[] array = new Integer[4];// 正常的聲明和建立一個數組
Student <?> [] stu= new Student[10];//聲明的時候使用泛型是沒有報名的
//Student <String> [] stu1 = new <String> Student[10];建立的時候也使用泛型那麼就會報錯
/*
* 你要是很想用泛型的數組 我們可以用ArrayList
*/
ArrayList<String> strList =new ArrayList<String>();//這樣說明這個連結清單隻能存放String類型的了 傳回的也是String類型的
strList.add(0, "a");
//strList.add(1, 1);存放數字就會報錯
String elem =strList.get(0);
System.out.println(elem);
/**
* 我們來模拟實作一下ArrayList的這個功能
*/
MyArrayList <Integer> strList1 =new MyArrayList<Integer>();
//strList1.add(0, "a");
strList1.addElement(0,1);
int elem1 =strList1.getElement(0);
System.out.println(elem1);
}
}
/**
* 模拟寫一個MyArrayList 很簡單的一個 隻是想簡單的說明一下那個思路
*
*/
class MyArrayList <E> {
//E[] cap =new E[10]; 沒有這樣的泛型數組
Object[] a = new Object[100];
public void addElement(int index,E e) {
a[index] = e;
}
public E getElement(int index) {
return (E)a[index];//強制轉換一下
}
public E[] getAll() {
return (E[])a;
}
}
4.泛型的聲明:
package testGeneric04;
import java.util.ArrayList;
import java.util.List;
/**
* JDK1.7中使用泛型,聲明一次類型即可
* 在使用或建立時不用指定類型
* @author Wang
*
*/
public class test {
public static void main(String[] args) {
List <String> list1= new ArrayList<String>();
List <String> list2= new ArrayList<>();
//list2.add(1);//添加一個 1 就會報錯 說明泛型限制了
List list3 = new ArrayList();//不加泛型的什麼都可以往裡面放;
list3.add(2);
list3.add("a");
}
}
5.泛型的嵌套:
package testGeneric04;
/**
* 泛型的嵌套
* @author Wang
* 拆分的時候要從外到内
*/
public class doubleGeneric <T> {
T student;
public static void main(String[] args) {
doubleGeneric <Student<String>> d = new doubleGeneric(); //意思就是 第一個類的泛型是Student類型 然受Student類的泛型是String類型
//拆分的過程如下;
d.student = new Student <String> ();
Student<String> student = d.student;
String score = student.score;
System.out.println(score);
}
}
6.通配符:
package testGeneric04;
/**
*
* 通配符
* ?類型不定,使用時确定類型
* ?使用:聲明類型或聲明方法上,類不能聲明或使用 通配符
* 通配符 ? 的extends : <= 他繼承的通配符 指定類型 子類或自身 (這裡跟子類 繼承 父類或實作接口 相反的)
* 通配符 ? 的super : >= 他super的通配符 指定類型 為自身或父類
*
* @author wang
*
*/
public class Student <T> {
T name;
T score;
public static void main(String[] args) {
Student<?> stu = new Student<Fruit>();
test(new Student<Integer>()); //使用時确定類型
//test4(stu); //使用時沒有确定類型
test3(new Student<Apple>());
//test2(new Student<Apple>()); //泛型沒有多态
//test4(new Student<Apple>()); // < Fruit
stu = new Student<Fruit>();;
test4(new Student<Object>());
test4(new Student<Fruit>());
}
public static void test(Student<?> stu){
}
public static void test2(Student<Fruit> stu){
}
public static void test3(Student<? extends Fruit> stu){//student 的通配符 <= Fruit 也就是說可以是Apple
}
public static void test4(Student<? super Fruit> stu){//student 的通配符 >= Fruit 也就是說可以是Object
}
}