泛型(贴标签)

2019-05-19  本文已影响0人  御都
public static void main(String[] args) {
        //创建集合对象,<String>
        ArrayList<String> alist = new ArrayList<String>();
        //添加集合元素
        alist.add("hello");
        alist.add("java");
        alist.add("world");
        //调用迭代器
        Iterator<String> it = alist.iterator();
        while(it.hasNext()){
            String s = it.next();//不用进行 强制类型转换
            System.out.println(s);
        }
        
    }

【例子2】

public class Test {
    public static void main(String[] args) {
        ObjectTool<String> ot = new ObjectTool<String>();
        ot.set("hello");
//The method set(String) in the type ObjectTool<String> is not applicable for the arguments (int)
//      ot.set(123);
        String s = ot.get();
        System.out.println(s);
        
    }
    
}
class ObjectTool <E>{
    private E e;

    public E get(){
        return e;
    }
    public void set(E e){
        this.e = e;
    }
    
}

3.1.2 泛型定义在接口或者类上,接口的实现类或者类的子类在定义时也许也不知道以哪种引用类型来实现就需要继承泛型。

public class Test {
    public static void main(String[] args) {
        ObjectTool<String> ot = new Demo<String>();
        ot.show("hello");
        
    }
    
}
interface ObjectTool<T> {
     void show(T msg);
    
}
class Demo<T> implements ObjectTool<T>{

    @Override
    public void show(T msg) {
        // TODO Auto-generated method stub
        System.out.println(msg);
    }

}

3.2 泛型定义在方法上,同类/接口上定义的泛型相互独立
作用:减少了参数列表数量相同的重写方法
调用方法时不用关注标签的佩戴,直接传入任意类型的引用变量即可
3.2.1 格式 <E/T/K/V> 返回值类型 方法名(E/T/K/V m){方法体}
【例子】

public class Test {
    public static void main(String[] args) {
        ObjectTool ot = new ObjectTool();
        ot.show("java");
        ot.show(new Integer(20));
        ot.show(new Boolean(true));
        
    }
    
}
class ObjectTool{
    public <T> void show(T msg){
        System.out.println(msg);
    }
    
}

4 泛型的通配符
泛型通配符<?>---表示任意类型的引用变量
<? extends E>--向下限定,E及其子类
<? super E>--向上限定,E及其父类

public class Test {
    //1 泛型如果明确写出来了,前后必须一致
    ObjectTool<Animal> ob = new Demo<Animal>();
    //Type mismatch: cannot convert from Demo<Cat> to ObjectTool<Animal>
//  ObjectTool<Animal> ob2 = new Demo<Cat>();
//  ObjectTool<Animal> ob3 = new Demo<Dog>();
    
    //2 泛型通配符<?>---表示任意类型的引用变量
    ObjectTool<?> ob3 = new Demo<Cat>();
    ObjectTool<?> ob4 = new Demo<Dog>();
    
    //<? extends E>--向下限定,E及其子类
    ObjectTool<? extends Animal> ob5 = new Demo<Animal>();
    //Type mismatch: cannot convert from Demo<Animal> to ObjectTool<? extends Dog>
//  ObjectTool<? extends Dog> ob6 = new Demo<Animal>();
//  ObjectTool<? extends Cat> ob7 = new Demo<Animal>();
    
    //3  <? super E>--向上限定,E及其父类
    ObjectTool<? super Animal> ob8 = new Demo<Animal>();
    //Type mismatch: cannot convert from Demo<Animal> to ObjectTool<? extends Dog>
//  ObjectTool<? super Animal> ob9 = new Demo<Cat>();
    ObjectTool<? super Cat> ob10 = new Demo<Animal>();
    
    
}
class ObjectTool<T>{
    
}
class Demo<T> extends ObjectTool<T>{
    
}
class Animal{}
class Cat extends Animal{}
class Dog extends Animal{}
上一篇 下一篇

猜你喜欢

热点阅读