• 泛型方法前为什么要加<T>


    package com.test05.myTest;
    
    
    class Fruit {     public String toString() {    return "Fruit"; } }
    
    class Apple extends Fruit {    public String toString(){ return "Apple";    } }
    
    class Person {    public String toString(){    return "Person";    } }
    
    class ClassName<T> {//主类,文件名ClassName.java
         
    
         void show_1(T t){
            System.out.println("show_1  "+ t.toString());
        }
        
        <E> void show_2(E e){
            System.out.println("show_2  "+e.toString());
        }
        
        <T> void show_3(T t){
            System.out.println("show_3  "+t.toString());
        }
        
        
    
        public static void main(String[] args) {
            ClassName<Fruit> o = new ClassName<Fruit>();
            Fruit f = new Fruit();
            Apple a = new Apple();
            Person p = new Person();
            System.out.println("show_1 演示________________________");
            o.show_1( f );
            o.show_1( a );
    //        o.show_1( p );  楼主把这行代码去掉注释看一下,是不能编译通过的。因为在
    //        ClassName<Fruit>中已经限定了全局的T为Fruit,所以不能再加入Person;
            System.out.println("show_2 演示________________________");
            o.show_2( f );
            o.show_2( a );
            o.show_2( p );
            System.out.println("show_3 演示________________________");
            o.show_3( f );
            o.show_3( a );
            o.show_3( p );
            
        }
    }

    输出:

    show_1 演示________________________
    show_1 Fruit
    show_1 Apple
    show_2 演示________________________
    show_2 Fruit
    show_2 Apple
    show_2 Person
    show_3 演示________________________
    show_3 Fruit
    show_3 Apple
    show_3 Person

    /* 而show_2 和show_3方法其实是完完全全等效的。意思就是说ClassName<T>中一旦
      T被指定为Fruit后那么show_1没有前缀<T> 的话,该方法中只能是show_1 (Fruit对象)


    而你要是有前缀<T>或<E>的话,那么你就是告诉编译器对它说:这是我新指定的一个类型,
    跟ClassName<T>类对象中的T没有半毛钱的关系。也就是说这个show_3中的T和show_2中的
    E是一个效果,也就是你可以把show_3同等程度地理解为<E> void show_3(E e){~~~~~}

    从上面我说的看,那就是 这个方法返回值前也加个<T>的话,这个T就代表该方法自己独有的某个类,而不去和类中限定的T产生冲突,你直接换成<E>会更容易理解的*/


    <T> 你可以理解为一个类型的声明,否则你的返回值和函数参数中突然出现了一个"T",编译器知道这是什么东西,肯定会报错,所以要从编译器的角度来理解这个问题.
    就跟int a;以后才能使用a这个变量一个道理

  • 相关阅读:
    解决中文环境下zabbix监控图形注释乱码
    SSIS CDC(Change Data Capture)组件在数据库中启用报错。 The error returned was 14234: 'The specified '@server' is invalid
    Tableau 群集部署
    访问Tableau自带的PostgreSQL数据库
    [译]Stairway to Integration Services Level 18 – 部署和执行
    [译]Stairway to Integration Services Level 16 – Flexible Source Locations (多文件导入)
    [译]Stairway to Integration Services Level 15 – SSIS 参数回顾
    [译]Stairway to Integration Services Level 14
    [译]Stairway to Integration Services Level 13
    [译]Stairway to Integration Services Level 12
  • 原文地址:https://www.cnblogs.com/winkey4986/p/3158760.html
Copyright © 2020-2023  润新知