一 概述
泛型,即“参数化类型”。就是将类型由原来的具体的类型参数化,类似于方法中的变量参数,此时类型也定义成参数形式,然后在使用/调用时传入具体的类型。
泛型的本质是为了参数化类型(在不创建新的类型的情况下,通过泛型指定的不同类型来控制形参具体限制的类型)。
操作的数据类型被指定为一个参数,这种参数类型可以用在类、接口和方法中,分别被称为泛型类、泛型接口、泛型方法。
二 泛型存在的意义
先看一个例子
public class Test {
double add(int a,int b){
return a+b;
}
double add (double a, double b){
return a+b;
}
public static void main(String args[]){
Test test = new Test();
System.out.println(test.add(2d,3d) +"");
System.out.println(test.add(2,3) +"");
List list = new ArrayList();
// List list<String> = new ArrayList();
list.add("test");
list.add(1);
}
}
先看add(),对于形参来说,可能有多种类型,但是不能因为要传入多种不同的类型就写多个重载方法,所以泛型的作用就体现了,可以使得形参参数类型化。再看常用的List,如果不指定泛型,可以添加各种类型的数据进去,编译器也不会报错,只有在运行期间才会报错。
三 泛型的使用
3.1 泛型类的使用
public class GenericClass<T> {
T data;
void setData(T data){
this.data = data;
}
void show(){
System.out.println("泛型的类型:"+data+"");
}
public static void main(String args[]){
GenericClass<String> genericClass1 = new GenericClass<>();
genericClass1.setData("String类型");
genericClass1.show();
//需要注意的是,泛型不能传入基本数据类型,如int,double等
GenericClass<Integer> genericClass2 = new GenericClass<>();
genericClass2.setData(1);
genericClass2.show();
}
}
运行结果
泛型的类型:String类型
泛型的类型:1
可以看出,T类型的泛型 可以传入String,Interger类型,从而实现对形参的扩展,但是不能传入基本数据类型,编译器也会检查出来。
3.2 泛型接口的使用
首先,定义一个接口
public interface GenericInterface<T> {
T getData();
}
定义一个类去实现这个接口
public class GenericInterface1<T> implements GenericInterface<T> {
T data;
void setData(T data){
this.data = data;
}
@Override
public T getData() {
return data;
}
public static void main(String args[]){
GenericInterface1<String> genericInterface1 = new GenericInterface1<>();
genericInterface1.setData("String类型");
System.out.println(genericInterface1.getData());
GenericInterface1<Integer> genericInterface2 = new GenericInterface1<>();
genericInterface2.setData(1);
System.out.println(genericInterface2.getData());
}
}
运行结果:
String类型
1
3.3 泛型方法的使用
public class GenericMethod {
static class Person {
@NonNull
@Override
public String toString() {
return "Person";
}
}
static class Man extends Person{
@NonNull
@Override
public String toString() {
return "man";
}
}
static class Cat{
@NonNull
@Override
public String toString() {
return "cat";
}
}
static class GenericClass<T>{
//非泛型方法,泛型方法必须用<T>来修饰
public void show1(T t){
System.out.println(t.toString());
}
public <T> void show02(T t){
System.out.println(t.toString());
}
public <H> void show03(H h){
System.out.println(h.toString());
}
}
public static void main(String args[]){
Person person = new Person();
Man man = new Man();
Cat cat = new Cat();
//限定了参数类型,必须是Peson或者其派生类 但是并非是泛型方法
GenericClass<Person> genericClass = new GenericClass<>();
genericClass.show1(person);
genericClass.show1(man);
//泛型方法的参数类型在使用时指定,和创建时限定的Person无关
genericClass.show02(man);
genericClass.show02(cat);
genericClass.show03(cat);
genericClass.show03(man);
}
}
四 泛型上下边界
在使用泛型时,可以对传入的泛型类型实参进行上下边界限制。
public class GenericMethod {
static class Person extends Animal {
@NonNull
@Override
public String toString() {
return "Person";
}
}
static class Man extends Person{
@NonNull
@Override
public String toString() {
return "man";
}
}
static class Cat extends Animal{
@NonNull
@Override
public String toString() {
return "cat";
}
}
static class Animal{
@NonNull
@Override
public String toString() {
return "animal";
}
}
static class GenericClass<T> {
T t;
}
static class GenericTest{
void printPersonOrSon(GenericClass<? extends Person> obj){
System.out.println(obj.t.toString());
}
void printCatOrParent(GenericClass<? super Cat> obj){
System.out.println(obj.t.toString());
}
}
public static void main(String args[]){
Person person = new Person();
Man man = new Man();
Cat cat = new Cat();
Animal animal = new Animal();
GenericClass<Person> personGenericClass = new GenericClass<>();
GenericClass<Man> manGenericClass = new GenericClass<>();
GenericClass<Cat> catGenericClass = new GenericClass<>();
GenericClass<Animal> animalGenericClass = new GenericClass<>();
personGenericClass.t = person;
manGenericClass.t = man;
catGenericClass.t = cat;
animalGenericClass.t = animal;
GenericTest test = new GenericTest();
test.printPersonOrSon(personGenericClass);
test.printPersonOrSon(manGenericClass);
//编译不通过,extends指定了上界,必须是Person或者其派生类
// test.printPersonOrSon(animalGenericClass);
// test.printPersonOrSon(catGenericClass);
test.printCatOrParent(catGenericClass);
test.printCatOrParent(animalGenericClass);
//编译不通过,super指定下届,必须是Cat的父类
// test.printCatOrParent(personGenericClass);
// test.printCatOrParent(manGenericClass);
}
}
五 关于虚拟机如何实现泛型
Java泛型是Java1.5之后才引入的,为了向下兼容,采用泛型擦除机制。Object类是所有类的父类,当在编译期时,会把所有的泛型转换成Object类型。
例如:
static class GenericClass<T> {
T t;
}
//编译完成后
static class GenericClass<Object> {
Object t;
}
网友评论