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
}
}