泛型

普通类的多态

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]; //返回时再转回去
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在之前的文章中分析过了多态,可以知道多态本身是一种泛化机制,它通过基类或者接口来设计,使程序拥有一定的灵活性,但是...
    _小二_阅读 3,971评论 0 0
  • 泛型为集合提供了编译时类型检查。 23、不要在代码中使用原生态类型 声明中具有一个或多个类型参数的类或接口统称为泛...
    Alent阅读 4,485评论 6 2
  • 泛型是Java 1.5引入的新特性。泛型的本质是参数化类型,这种参数类型可以用在类、变量、接口和方法的创建中,分别...
    何时不晚阅读 8,166评论 0 2
  • 附上思维导图。这篇博客主要讲了如下知识点。 看完了《Thinking in Java》的第十五章泛型,着实被震了一...
    Happioo阅读 4,453评论 0 1
  • 让我住在你心里吧, 这样我就知道你想什么了啊!
    我是小坏蛋阅读 656评论 0 0

友情链接更多精彩内容