JDK5-泛型

泛型

    public static void main(String[] args) {
        //创建集合对象
        ArrayList array = new ArrayList();

        //添加元素
        array.add("hello");
        array.add("world");
        array.add("java");
        //array.add(new Integer(100));
        array.add(100);//JDK 5 以后自动装箱
        //等价于  array.add(Integer.valueOf(100));

        //遍历
        Iterator it = array.iterator();
        while(it.hasNext()){
        //此处运行时报错,因为100是Integer类型的,不能向下转型成String类型
            String s = (String) it.next();
            System.out.println(s);

        }

    }

泛型:是一种把类型明确的工作推迟发哦创建对象或调用方法的时候才去明确的特殊类型参数化类型,把类型当做参数一样的传递。

格式: <数据类型> 此处的数据类型只能是引用类型

泛型改进

public static void main(String[] args) {
        //用ArrayList 存储字符串元素,并遍历,用泛型改进、
        ArrayList<String> array= new ArrayList<String>();

        array.add("hello");
        array.add("world");
        array.add("java");

        //迭代器是依赖集合存在的,集合是什么类型,迭代器就是什么类型。
        Iterator<String> it = array.iterator();
        while(it.hasNext()){
            String s = it.next();
            System.out.println(s);


        }
        System.out.println();
        for(int x = 0;x<array.size();x++){
            String s = array.get(x);
            System.out.println(s);
        }
    }

自定义对象的存储和遍历

//创建集合对象
        ArrayList<Student> array = new ArrayList<Student>();

        //添加元素
        Student s1 = new Student("诸葛亮",23);
        Student s2 = new Student("江建",28);
        Student s3 = new Student("曹操",27);

        array.add(s1);
        array.add(s2);
        array.add(s3);
        //遍历
        Iterator<Student> it = array.iterator();
        while(it.hasNext()){
            Student s = it.next();
            System.out.println(s.getName()+"   "+s.getAge());

        }
        System.out.println();
        //for()
        for(int x = 0 ;x<array.size();x++){
            Student s = array.get(x);
            System.out.println(s.getName()+"   "+s.getAge());
        }

好处:

  • 把运行时期的问题提前到了编译期间
  • 避免了强制类型转换
  • 优化了程序设计,解决了黄色警告线

使用场所:如果类,接口,抽象类后面跟的有<E>就要使用泛型, 一般是在集合中使用

泛型的引入

早起的时候,用Object类来代表任意的类型
向上转型是没有任何问题的,但是向下转型的时候其实隐含了类型转换的问题。
也就是说,这样的程序并不是安全的,所以java在JDK5以后引入了泛型,提高程序的安全性。

public class ObjectTool {
    private Object obj;
    public Object getObj(){
        return obj;
    }
    public void setObj(Object obj){
        this.obj = obj;
    }

}
        ObjectTool ot = new ObjectTool();
        ot.setObj(new String("fangwing"));//Object obj = new String(" ");向上转型
        String s = (String) ot.getObj();
        System.out.println("xingisj:"+s);

        ot.setObj(new Integer(30));
        Integer i = (Integer) ot.getObj();
        System.out.println("jfioew:"+i);

        ot.setObj(new String("hewi"));//Object obj = new String(" ");向上转型
        Integer ii=(Integer)ot.getObj();//此处向下转型,编译器不报错,是因为Object可能装成Integer类型。

泛型应用

泛型类

  • 把泛型定义在类上
  • 格式:
    public class类名<泛型类1,泛型类2…>
    • Map<K,v>, Set<E>,
  • 注意:泛型类型必须是引用类型
public class ObjectTool<T> {
    private T obj;
    public T getObj(){
        return obj;
    }
    public void setObj(T obj){
        this.obj = obj;
    }

}
        ObjectTool<String> ot = new ObjectTool<String>();
        ot.setObj(new String("李"));
        String s = ot.getObj();
        System.out.println("姓名"+s);

        ObjectTool<Integer> ot2 = new ObjectTool<Integer>();
        ot2.setObj(new Integer(24));
        Integer i = ot2.getObj();
        System.out.println("年龄"+i);

ArrayList 集合的add(E e)方法

public class ObjectTool<T>{
    public void show(T t){
        System.out.println(t);
    }
}
ObjectTool<String> obj1=new ObjectTool<String>();
obj1.show("wang");
ObjectTool<Boolean> obj2 = new ObjectTool<Boolean>();
obj2.show(true);

引入:如果类上没有泛型的话,方法还能不能接收任意类型的参数?
泛型方法

  • 把泛型定义在方法上
  • 格式:public <泛型类型> 返回类型 方法名(泛型类型)
  • 好处:方法可以接收任意类型
public class ObjectTool{
    public <T> void show(T t){
        System.out.println(t);  
    }
}
ObjectTool ot = new ObjectToool();
ot.show("hello");//String类型
ot.show(100);//Integer类型
ot.show(true);//Boolean类型

泛型接口

  • 泛型定义在接口上
  • 格式:public interface 接口名<泛型类型1,泛型类型2… >
//接口
public interface Inter<T>{
    public abstract void show(T t);
}
//实现类
public class InterImpl<T> implements Inter<T>{
    public void show(T t){
        System.out.println(t);
    }
}

泛型 —通配符(三种)

  • ?: 表示任意的类型都可以
  • ? extends E : 向下限定,E及其子类
  • ? super E : 向上限定,E及其父类
public class GenericDemo {
    public static void main(String[] args) {
    //泛型如果明确写的时候,前后必须一致
        Collection<Object>  c1= new ArrayList<Object>();
//      Collection<Object>  c2= new ArrayList<Animal>();//编译错误
//      Collection<Object>  c3= new ArrayList<Dog>();//编译错误
//      Collection<Object>  c4= new ArrayList<Cat>();//编译错误


        //? 表示任意的类型都是可以的
        Collection<?>  c5= new ArrayList<Object>();
        Collection<?>  c6= new ArrayList<Animal>();
        Collection<?>  c7= new ArrayList<Cat>();
        Collection<?>  c8= new ArrayList<Dog>();

        //? extends E: 向下限定,E及其子类 
        //Collection <? extends animal> c9 = new ArrayList<Object>();//编译错误
        Collection<? extends Animal> c10 = new ArrayList<Animal>();
        Collection<? extends Animal> c11 = new ArrayList<Dog>();
        Collection<? extends Animal> c12 = new ArrayList<Cat>();

        //? super E : 向上限定,E及其父类    
        Collection<? super Animal> c13 = new ArrayList<Object>();
        Collection<? super Animal> c14 = new ArrayList<Animal>();
        //Collection<? super Animal> c15 = new ArrayList<Dog>();//编译错误
        //Collection<? super Animal> c16 = new ArrayList<Cat>();//编译错误
    }

}
class Animal{

}
class Dog extends Animal{

}
class Cat extends Animal{

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值