Java中泛型嵌套举例

package tk.zhangwei.Generic;

public class Student<T> {
    private T name;

    public T getName() {
        return name;
    }

    public void setName(T name) {
        this.name = name;
    }


}
package tk.zhangwei.Generic;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/***
 * 泛型嵌套
 *   从外到内
 * @author zw
 *
 */

public class Test02 {
    public static void main(String[] args){
        Student<String> stu = new Student<String>();
        stu.setName("张伟");
        System.out.println(stu.getName());


        PhStudent<Student<String>> ph = new PhStudent<Student<String>>();
        ph.setId(stu);
        stu = ph.getId();
        String name = stu.getName();
        System.out.println(name);

        //遍历

        Map<String,String> map = new HashMap<String,String>();
        map.put("a", "张伟");
        map.put("b", "老王");

        Set<Entry<String,String>> entrySet = map.entrySet();
        for(Entry<String,String> entry:entrySet){
            String Key = entry.getKey();
            String Value = entry.getValue();
            System.out.println(Key+"---"+Value);
        }


    }
}

666

注意:泛型没有多态,没有数组

package tk.zhangwei.Generic;

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

/***
 * 1.泛型 没有多态
 * 2.泛型没有数组
 * @author zw
 *
 */

public class Test01 {

    public static void main(String[] args) {
        //父类对象 直接引用子类对象
        Fruit f = new apple();

        //泛型 没有多态
        //List<Fruit> list = new Arraylist<apple>();

        //泛型没有数组
        //Fruit<String>[] arr = new Fruit<String>[10];

        //jdk1.7简化后
        List<Fruit> list2 = new ArrayList<>();

    }

}
package tk.zhangwei.Generic;

public class Fruit {

}
class apple extends Fruit{

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值