泛型接口和泛型方法

泛型接口的定义语法:

interface 接口名称 <泛型标识,泛型标识,…> {
    泛型标识 方法名(); 
    .....
}

泛型接口的使用

  • 实现类不是泛型类,接口要明确数据类型

  • 实现类也是泛型类,实现类和接口的泛型类行要保持一致

    小案例:

复制代码
 1 package com.genericity.demo1;
 2 
 3 /**
 4  * 泛型接口的实现类,是一个泛型类,那么要保证实现接口的泛型类泛型标识包含泛型接口的泛型标识
 5  */
 6 public class Pair<T,E> implements Generic<T>  {
 7     private T key;
 8     private E value;
 9 
10     public Pair(T key, E value) {
11         this.key = key;
12         this.value = value;
13     }
14 
15     @Override
16     public T getKey() {
17         return key;
18     }
19 
20     public E getValue(){
21         return value;
22     }
23 }
View Code
复制代码
复制代码
 1 package com.genericity.demo1;
 2 
 3 import com.sun.xml.internal.ws.api.ha.StickyFeature;
 4 
 5 public class MainClass {
 6     public static void main(String[] args) {
 7         ImplClass implClass = new ImplClass();
 8         String key1 = implClass.getKey();
 9         System.out.println(key1);
10 
11         System.out.println("========================================================");
12 
13         Pair<String, Integer> strIntegerPair = new Pair<>("today",1000);
14         String key = strIntegerPair.getKey();
15         Integer value = strIntegerPair.getValue();
16         System.out.println("key:"+key+" value:"+value);
17 
18 
19     }
20 }
View Code
复制代码
1 today is nice
2 ============================
3 key:today value:1000
View Code

泛型方法

泛型类,是在实例化类的时候指明泛型的具体类型。

泛型方法:是在调用方法的时候指明泛型的具体类型。

修饰符 <T,E, ...> 返回值类型 方法名(形参列表) { 
     方法体... 
}
1.public与返回值中间非常重要,可以理解为声明此方法为泛型方法。
2.只有声明了的方法才是泛型方法,泛型类中的使用了泛型的成员方法并不是泛型方法。
3.< T >表明该方法将使用泛型类型T,此时才可以在方法中使用泛型类型T。
4.与泛型类的定义一样,此处T可以随便写为任意标识,常见的如T、E、K、V等形式的参数常用于表示泛型。

小案例:

复制代码
 1 package com.genericity;
 2 
 3 
 4 import javax.lang.model.element.VariableElement;
 5 import java.util.ArrayList;
 6 import java.util.Random;
 7 
 8 public class ProductGetter<T> {
 9     //奖品
10     private T product;
11     //奖品池
12     ArrayList<T> list = new ArrayList<>();
13     //随机数生成器
14     Random random = new Random();
15 
16     //添加奖品
17     public void addProduct(T t){
18         list.add(t);
19     }
20 
21     /**
22      * 抽奖方法
23      * 注意:这个方法是不能够申明成static静态类型的
24      * @return
25      */
26     public T getProduct(){
27         product = list.get(random.nextInt(list.size()));
28         return product;
29     }
30 
31     /**
32      * 定义泛型方法
33      * @param list 参数 奖品列表
34      * @param <E> 泛型标识,具体类型,由调用方法的时候来指定
35      * @return
36      */
37     public <E> E getProduct(ArrayList<E> list){
38         return list.get(random.nextInt(list.size()));
39     }
40 
41     /**
42      * 静态泛型方法
43      * @param t
44      * @param k
45      * @param v
46      * @param <T>
47      * @param <K>
48      * @param <V>
49      */
50     public static <T,K,V>  void getType(T t,K k,V v){
51         System.out.println(t+":"+t.getClass().getSimpleName());
52         System.out.println(k+":"+k.getClass().getSimpleName());
53         System.out.println(v+":"+v.getClass().getSimpleName());
54     }
55 
56 
57 }
View Code
复制代码
复制代码
 1 package com.genericity;
 2 
 3 import java.util.ArrayList;
 4 
 5 public class Test2 {
 6     public static void main(String[] args) {
 7         //泛型类中的成员方法的使用
 8         ProductGetter<Integer> productGetter = new ProductGetter<>();
 9         ArrayList<Integer> list1 = new ArrayList<>();
10         list1.add(100);
11         list1.add(200);
12         list1.add(300);
13         //泛型方法的调用,类型是通过调用方法的时候来指定的
14         Integer product = productGetter.getProduct(list1);
15         System.out.println("product:"+product);
16 
17         System.out.println("----------------------------------------");
18         //泛型方法的调用,类型是通过调用方法的时候来指定的
19         ArrayList<String> strlist = new ArrayList<>();
20         strlist.add("笔记本电脑");
21         strlist.add("苹果手机");
22         strlist.add("扫地机器人");
23         String product1 = productGetter.getProduct(strlist);
24         System.out.println("product1:"+product1);
25 
26         System.out.println("----------------------------------------");
27         ArrayList<Integer> intlist = new ArrayList<>();
28         intlist.add(1000);
29         intlist.add(2000);
30         intlist.add(3000);
31         int product2 = productGetter.getProduct(intlist);
32         System.out.println("product1:"+product2);
33 
34 
35         System.out.println("----------------------------------------");
36         //调用多个泛型类型的静态泛型方法
37         ProductGetter.getType("扫地僧",100,true);
38 
39 
40 
41     }
42 }
View Code
复制代码
复制代码
1 product:200
2 ----------------------------------------
3 product1:笔记本电脑
4 ----------------------------------------
5 product1:1000
6 ----------------------------------------
7 扫地僧:String
8 100:Integer
9 true:Boolean
View Code
复制代码

泛型方法与可变参数

public <E> void print(E... e){
    for (E e1 : e) {
        System.out.println(e);
    }
}

小案例:

复制代码
 1    /**
 2      * 可变长参数的泛型方法
 3      * @param e
 4      * @param <E>
 5      */
 6     public static <E> void geVariable(E... e){
 7         for (int i = 0; i < e.length; i++) {
 8             System.out.print(e[i]+" ");
 9         }
10     }
View Code
复制代码
      //可变参数的泛型方法的调用
        ProductGetter.geVariable("a", "b", "c");

运行结果:a b c

泛型方法的总结:

  • 泛型方法能使方法独立于类而产生变化

  • 如果static方法要使用泛型能力,就必须使其成为泛型方法

    总的来说,在实际工作中,我们能使用泛型方法的,尽量使用泛型方法,不要使用泛型类。

posted on   ~码铃薯~  阅读(385)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
历史上的今天:
2019-12-08 mybatis框架-resultMap的自动映射级别-partial 和full的探讨
2019-12-08 mybatis框架-使用resultMap实现高级结果映射,collection属性的使用
2019-12-08 mybatis框架-使用resultMap实现高级结果映射,association属性
2019-12-08 前端知识--mulline框架,返回到前一个页面,并且刷新前一个页面

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示