JAVA-泛型



```java
package fanxing;

import java.util.ArrayList;
import java.util.List;

public class TestFanxin {

//    /**
//     * 1.? 无边界限定符 ? extends T 上边界限定符  ? super T 下边界限定符
//     * 2. T 表示确切的类型, ? 表示类型不确定  T 可以用于泛型类或者泛型方法定义 ? 不可以, T 只能extends 可以extends 多个接口,不能super,? 可以单个extend,可以super
//     * 3. 上读下写 上边界可读不能写,下边界可写但是不能读
//     * @param args
//     */

    public static void main(String[] args) {
        List<? extends Fruit> fruitList1 = new ArrayList<>();
        List<? super Fruit> fruitList2 = new ArrayList<>();
        List<Apple> appleList = new ArrayList<>();
        List<Fruit> fruitList = new ArrayList<>();
        List<Food> foodList = new ArrayList<>();
        Apple a = new Apple();
        Banana b = new Banana();
        Fruit fruit = new Fruit();
        Food food = new Food();
        Meet m = new Meet();
        fruitList2.add(a);
        fruitList2.add(b);
        fruitList2.add(fruit);
        fruitList1 = appleList;
        fruitList1 = fruitList;
//        Fruit f = fruitList1.get(0);
        Fruit f2 =(Fruit) fruitList2.get(0);
        System.out.println(f2);

    }
}


class Food {}

class Meet extends Food{}

class Fruit extends Food{}

class Beef extends Meet{}

class Apple extends Fruit{}

class Banana extends Fruit{}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值