JAVA中泛型讲解

package com.jiuqi.util;


//<? extends T>和<? super T>是Java泛型中的“通配符(Wildcards)”和“边界()Bbounds"的概念
//<? extends T> :是指上界通配符  表示类型的上限,表示参数化类型是T或者T的子类   上界<? extends T>不能往里存,只能往外取
//<? super T>:是指下界通配符  表示类型的下限,表示参数化类型T是此类型的超类型(父类型),直至Object 但往外取只能放在Object对象里
public class FanxingTest {
public static void main(String[] args) {
Plate<? extends Fruit> p=new Plate<Apple>(new Apple());
p.get().print();
Plate<? super Fruit> p2=new Plate<Fruit>();
p2.set(new Apple());

}
}


class Plate<T>{
private T item;
public Plate(){}
public  Plate(T t){
this.item = t;
}
public void set(T t){
this.item = t;
}
public T get(){
return item;
}

}


class Fruit{
public void print(){
System.out.println("水果");
}
}
class Apple extends Fruit{
public void print(){
System.out.println("苹果");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值