当前位置:   article > 正文

Java基础-泛型

Java基础-泛型

泛型的定义

  1. 类或接口上定义泛型
class Demo<T>{
    public void show(T t){
        
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
interface Demo<T>{
    public void show(T t)
}
  • 1
  • 2
  • 3
  1. 方法上定义泛型
class Demo{
    public <E> void show(E e){
    }
}
  • 1
  • 2
  • 3
  • 4

注意:如果类和方法上同时定义了泛型,如下

class Demo<T>{
    public <T> void show(T t){
    }
}
  • 1
  • 2
  • 3
  • 4

方法上的泛型和类上的泛型就无关了,换句话,同名的情况下,方法上的优先。上面的定义等同

class Demo<T>{
    public <E> void show(E e){
    }
}
  • 1
  • 2
  • 3
  • 4

使用示例

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 Demo<T>{
 
    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) {
        Demo<Fruit> o = new Demo<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 );
         
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

演示结果

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

从show_2 和show_3来看可以证明上面的概述,show_2和show_3传入的对象不局限于Fruit

泛型的上限和下限

  • 指明了泛型的上限为Fruit以及Fruit的子类
class Fruit { 
    public String toString() { 
       return "Fruit"; 
    }
}
 
class Apple extends Fruit {
    public String toString(){
        return "Apple";   
    }
}

class RedApple extends Apple {
    public String toString(){
        return "RedApple";   
    }
}

//正确示例
//T 可以为Fruit和其子类
class Demo<T extends Fruit>{
    void show(T t){
        System.out.println("show : "+ t.toString());
    }
    public static void main(String[] args) {
      Demo demo =new Demo();
      demo.show(new Fruit());
      demo.show(new Apple()); 
      demo.show(new RedApple());
    }
}

//错误示例
//T 可以为Apple和其子类,Fruit属于Apple 父类
class Demo2<T extends Apple>{
    void show(T t){
        System.out.println("show : "+ t.toString());
    }
    public static void main(String[] args) {
      Demo2 demo =new Demo2();
      //错误 Demo2制定了上限为Apple 
      //demo.show(new Fruit());
      demo.show(new Apple()); 
      demo.show(new RedApple());
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 指明了泛型的下限
class Fruit { 
    public String toString() { 
       return "Fruit"; 
    }
}
 
class Apple extends Fruit {
    public String toString(){
        return "Apple";   
    }
}

class RedApple extends Apple {
    public String toString(){
        return "RedApple";   
    }
}

class Demo<T super Fruit>{
    void show(T t){
        System.out.println("show : "+ t.toString());
    } 

    public static void main(String[] args) {
      Demo demo =new Demo();
      demo.show(new Fruit());
      //错误,demo制定了下限为Fruit
      //demo.show(new Apple());
      //demo.show(new RedApple()); 
    }
}

class Demo<T super Apple>{
    void show(T t){
        System.out.println("show : "+ t.toString());
    } 

    public static void main(String[] args) {
      Demo demo =new Demo();
      demo.show(new Fruit());
      demo.show(new Apple());
      //错误,demo制定了下限为Apple
      //demo.show(new RedApple()); 
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/306671
推荐阅读
相关标签
  

闽ICP备14008679号