zoukankan      html  css  js  c++  java
  • 泛型里面的<T> List<T>前面的<T>代表是什么意思?为什么要加<T>? <T> T

    import java.util.*;
    
    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
    1. import java.util.*;
    2.  
       
    3.  
      class Fruit { public String toString() { return "Fruit"; } }
    4.  
       
    5.  
      class Apple extends Fruit { public String toString(){ return "Apple"; } }
    6.  
       
    7.  
      class Person { public String toString(){ return "Person"; } }
    8.  
       
    9.  
      class ClassName<T> {//主类,把你文件名改成ClassName.java
    10.  
       
    11.  
       
    12.  
      void show_1(T t){
    13.  
      System.out.println("show_1 "+ t.toString());
    14.  
      }
    15.  
       
    16.  
      <E> void show_2(E e){
    17.  
      System.out.println("show_2 "+e.toString());
    18.  
      }
    19.  
       
    20.  
      <T> void show_3(T t){
    21.  
      System.out.println("show_3 "+t.toString());
    22.  
      }
    23.  
       
    24.  
       
    25.  
       
    26.  
      public static void main(String[] args) {
    27.  
      ClassName<Fruit> o = new ClassName<Fruit>();
    28.  
      Fruit f = new Fruit();
    29.  
      Apple a = new Apple();
    30.  
      Person p = new Person();
    31.  
      System.out.println("show_1 演示________________________");
    32.  
      o.show_1( f );
    33.  
      o.show_1( a );
    34.  
      // o.show_1( p ); 楼主把这行代码去掉注释看一下,是不能编译通过的。因为在
    35.  
      // ClassName<Fruit>中已经限定了全局的T为Fruit,所以不能再加入Person;
    36.  
      System.out.println("show_2 演示________________________");
    37.  
      o.show_2( f );
    38.  
      o.show_2( a );
    39.  
      o.show_2( p );
    40.  
      System.out.println("show_3 演示________________________");
    41.  
      o.show_3( f );
    42.  
      o.show_3( a );
    43.  
      o.show_3( p );
    44.  
       
    45.  
      }
    46.  
      }
    47.  
      程序输出:
    48.  
      show_1 演示________________________
    49.  
      show_1 Fruit
    50.  
      show_1 Apple
    51.  
      show_2 演示________________________
    52.  
      show_2 Fruit
    53.  
      show_2 Apple
    54.  
      show_2 Person
    55.  
      show_3 演示________________________
    56.  
      show_3 Fruit
    57.  
      show_3 Apple
    58.  
      show_3 Person
  • 相关阅读:
    消费者端调用服务者端的接口,服务端抛出一个自定义异常,该异常继承了RuntimeException,但是消费者端Debug发现catch到的是RpcException,RpcExcetion为dubbo的异常,为什么服务提供者的返回的是自定义异常却变成了RpcException?
    springcloud使用常见总是总结
    mysql启动时报 --initialize specified but the data directory has files in it. Aborting.
    简介
    继承&&聚合
    maven常见命令&&依赖&&maven生命周期
    下载配置Maven&&使用maven
    Maven [ERROR] 不再支持源选项 5。请使用 6 或更高版本
    解决Maven下载速度缓慢问题
    配置和使用Maven
  • 原文地址:https://www.cnblogs.com/lishanyang/p/15173739.html
Copyright © 2011-2022 走看看