泛型

作者: VikingOldYoung | 来源:发表于2016-10-29 21:05 被阅读0次

普通类的多态

public class FruitApp {
    public static void main(String[] args) {
        Fruit f =new Apple();   //声明时使用多态
        test01(new Apple());  //方法里形参使用多态
    }
    public static void test01(Fruit fruit){  //形参使用多态
    }
    public static Fruit test02(){  
        return new Apple();   //返回值使用多态
    }
}

对泛型,是没有多态的

/*定义泛型类*/
public class A<T> {
}

public class App {
    public static void main(String[] args) {
        //A<Fruit> a=new A<Apple>();泛型没有多态
        A<Fruit> a=new A<Fruit>();
        //test01(new A<Apple>());//形参使用多态错误
    }
    public static void test01(A<Fruit> a){  
    }
    public static A<Fruit> test02(){  
        //return new A<Apple>;   //返回值使用多态错误
    }
}

利用通配符,可以实现上面想实现的功能

public class Student<T> {
    T score;
    
    public static void main(String[] args) {
        Student<?> stu=new Student<String>(); //声明时使用通配符相当于泛型了,同下面的一样都是在声明时
        test01(new Student<Integer>());  
        test02(new Student<Apple>()); 
        //test03(new Student<Apple>()); 错误,泛型没有多态
    }
    
    public static void test01(Student<?> a){ //test01就可以传入任意指定类型的类了
        
    }
    /*此处的test02 则可以传入Fruit的子类,实现了多态*/
    public static void test02(Student<?extends Fruit> a){
    
    }
    /*该方法已经声明了泛型的具体属性,在上面调用的时候是没办法多态的*/
    public static void test03(Student<Fruit> a){
        
    }
}

泛型的嵌套

public class AhAhut<T> {
    T stu;
    
    public static void main(String[] args) {
        //泛型的嵌套,下面定义的意思是,AhAhut里面的stu定义为了Student类,而因为
        //Student类也有一个泛型需要具体指定,所以就有了嵌套
        AhAhut<Student<String>> room=new AhAhut<Student<String>>();
        //要取出的话,需要从外到内拆分,先取出Student,再取score
        room.stu=new Student<String>();//上面已经指定了Student的具体类型
        Student<String> stu=room.stu;
        String score=stu.score;
        System.out.println(score);
    }
}

输出结果为:null

没有泛型数组,用通配符可以去替代

    public static void main(String[] args) {
        Integer[] arr=new Integer[10]; //普通数组
                //没有泛型数组
                //Student<String>[] arr2=new Student<String>[10];
        Student<?>[] arr2=new Student<?>[10];//可以用问号来创建
                //第一个Student的泛型指定为String
        Student<String> stu=new Student<>();
        stu.score="A";
        arr2[1]=stu;
        System.out.println(arr2[1].score);
                //第一个Student的泛型指定为Integer
        Student<Integer> stu2=new Student<>();
        stu2.score=20;
        arr2[2]=stu2;
        System.out.println(arr2[2].score);
    }
}

用容器去完成

public class Array {
   
    public static void main(String[] args) {
        //具体指定MyArrayList为String
        MyArrayList<String> strlist=new MyArrayList<String>();
        strlist.add(0, "abc");
        String elem=strlist.get(0);
        System.out.println(elem);
    }
}
/*可以定义一个容器来实现泛型数组的功能*/
class MyArrayList<E>{
    //E[] cap=new E[];不能创建泛型数组,
    //只能用Object来存,取的时候再转回去
    Object[] cap=new Object[10];
    
    public void add(int idx,E e){
        cap[idx]=e; //Object数组可以接收任意类型,所以E类型能进来
    }
    
    @SuppressWarnings("unchecked")
    public E[] getAll(){
        return (E[]) cap; //返回时再转回去
    }
    @SuppressWarnings("unchecked")
    public E get(int idx){
        return (E)cap[idx]; //返回时再转回去
    }
}

相关文章

  • 泛型 & 注解 & Log4J日志组件

    掌握的知识 : 基本用法、泛型擦除、泛型类/泛型方法/泛型接口、泛型关键字、反射泛型(案例) 泛型 概述 : 泛型...

  • 【泛型】通配符与嵌套

    上一篇 【泛型】泛型的作用与定义 1 泛型分类 泛型可以分成泛型类、泛型方法和泛型接口 1.1 泛型类 一个泛型类...

  • 泛型的使用

    泛型有三种使用方式,分别为:泛型类、泛型接口、泛型方法 泛型类 泛型接口 泛型通配符 泛型方法 静态方法与...

  • Java 泛型

    泛型类 例如 泛型接口 例如 泛型通配符 泛型方法 类中的泛型方法 泛型方法与可变参数 静态方法与泛型 泛型上下边...

  • 探秘 Java 中的泛型(Generic)

    本文包括:JDK5之前集合对象使用问题泛型的出现泛型应用泛型典型应用自定义泛型——泛型方法自定义泛型——泛型类泛型...

  • Web笔记-基础加强

    泛型高级应用 自定义泛型方法 自定义泛型类 泛型通配符? 泛型的上下限 泛型的定义者和泛型的使用者 泛型的定义者:...

  • 重走安卓进阶路——泛型

    ps.原来的标题 为什么我们需要泛型? 泛型类、泛型接口和泛型方法(泛型类和泛型接口的定义与泛型方法辨析); 如何...

  • Kotlin泛型的高级特性(六)

    泛型的高级特性1、泛型实化2、泛型协变3、泛型逆变 泛型实化 在Java中(JDK1.5之后),泛型功能是通过泛型...

  • Java 19-5.1泛型

    泛型类定义泛型类可以规定传入对象 泛型类 和泛型方法 泛型接口 如果实现类也无法确定泛型 可以在继承类中确定泛型:

  • 【Swift】泛型常见使用

    1、Swift泛型4种 泛型函数泛型类型泛型协议泛型约束 2、泛型约束3种 继承约束:泛型类型 必须 是某个类的子...

网友评论

      本文标题:泛型

      本文链接:https://www.haomeiwen.com/subject/vvyyuttx.html