一、 泛型边界
!extends ------- 就用继承关键词声明类型边界
泛型边界:泛型参数指定范围(就...你总不能乱传吧,还是得有个度来着...)
代码:(只能生产车的制车厂)
BenzCar类:(extends Car)
类似于BMWCar
package fanxing;
/**
* @author: y9
* @Date: 2021/10/02
*/
public class BenzCar extends Car{
public BenzCar() {
System.out.println("开始生产奔驰汽车");
}
}
核心:
public class BenzCar extends Car{
public BenzCar() {
System.out.println("开始生产奔驰汽车");
}
}
CarFactory类:
package fanxing;
/**
* @author: y9
* @Date: 2021/10/02
*/
public class CarFactory <T extends Car>{
public T create(Class<T> clazz) throws Exception {
System.out.println("制车厂:装载发动机、座椅、轮子");
return clazz.newInstance();
}
}
测试类:
package fanxing;
/**
* @author: y9
* @Date: 2021/10/02
*/
public class GenericTest <T>{
public static void main(String[] args) throws Exception {
CarFactory<BenzCar> benz = new CarFactory<BenzCar>();
CarFactory<BMWCar> bmw = new CarFactory<BMWCar>();
System.out.println("======开始生产奔驰汽车======");
benz.create(BenzCar.class);
System.out.println("======开始生产宝马汽车======");
bmw.create(BMWCar.class);
}
}
结果:
======开始生产奔驰汽车======
制车厂:装载发动机、座椅、轮子
开始生产奔驰汽车
======开始生产宝马汽车======
制车厂:装载发动机、座椅、轮子
开始生产宝马汽车
二、 含边界的泛型方法
代码:
package fanxing;
/**
* @author: y9
* @Date: 2021/10/02
*/
public class Sortor {
public<V extends Integer>V getMax(V v, V s){
//return math.max(v,s); //报错返回类型
if(v>s) return v;
else return s;
}
}
三、 多边界
!extends Obj1 & Obj2 ------- 就用继承关键词+&声明多类型边界
代码:
package fanxing;
/**
* @author: y9
* @Date: 2021/10/02
*/
public class Factory<T extends Speakable&Flyable> {
public T create(Class<T> t) throws Exception{
return t.newInstance();
}
}
public class Parrot implements Speakable,Flyable{
@Override
public String speak() {
return "我是一只鹦鹉";
}
}
package fanxing;
public class GenericTest <T>{
public static void main(String[] args) throws Exception {
Factory<Parrot> factory = new Factory<Parrot>();
Parrot parrot = factory.create(Parrot.class);
System.out.println(parrot.speak());
}
}
结果:
我是一只鹦鹉
四、 通配符与边界
! ? extends/super Obj ------- 就用?+ 继承关键词声明边界
代码:
package fanXin2;
/**
* @author: y9
* @Date: 2021/10/02
*/
public class Animal {
}
public class Bird extends Animal{
}
public class Fish extends Animal{
}
package fanXin2;
/**
* @author: y9
* @Date: 2021/10/02
*/
public class Zoo<T> {
private T t;
public Zoo(T t) {
this.t = t;
}
public T pop() {
return this.t;
}
}
方式一:用extends(限制子类——
package fanXin2;
/**
* @author: y9
* @Date: 2021/10/02
*/
public class GenericTest {
public static void main(String[] args) {
Zoo<? extends Animal> zoo = new Zoo<Bird> (new Bird());
//初始设置为Bird,后续可更改,但必须继承Animal类
System.out.println(zoo.pop().getClass().toString());
zoo = new Zoo<Fish>(new Fish()); //修改为Fish
System.out.println(zoo.pop().getClass().toString());
}
}
核心:
先创建Zoo类实例为< ? extends Animals>,可后续修改其泛型类型,但必须继承Animal类。
初始化为Bird类。
后续可修改为Fish类。
结果:
class fanXin2.Bird
class fanXin2.Fish
方式二: 用super (限制父类——
package fanXin2;
/**
* @author: y9
* @Date: 2021/10/02
*/
public class GenericTest {
public static void main(String[] args) {
Zoo<? super Bird> zoo = new Zoo<Bird> (new Bird());
//初始设置为Bird,后续可更改,但必须继承Animal类
System.out.println(zoo.pop().getClass().toString());
zoo = new Zoo<Animal>(new Animal());
System.out.println(zoo.pop().getClass().toString());
//zoo = new Zoo<Fish>(new Fish()); //不合法,Fish不是Bird的父类或本类
}
}
结果:
class fanXin2.Bird
class fanXin2.Animal