【java高级程序设计】泛型(中)||边界||自食用

本文详细介绍了Java中泛型的边界使用,包括单一边界、含边界的泛型方法、多边界以及通配符与边界的结合。通过示例展示了如何限制泛型类型的上下界,确保类型安全,并在实际的工厂类和排序方法中应用这些概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、 泛型边界

!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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值