JAVA泛型之泛型方法的定義和使用方式
1.泛型方法的定義和語法
1.1 定義
泛型方法 是在 調(diào)用方法 的時(shí)候指明泛型的具體類型。
【泛型方法 能夠使方法獨(dú)立于類的處理指定的類型。】
1.2 語法
修飾符 <T,E,…> 返回值類型 方法名(形參列表){ 。。。。。。 }
修飾符與返回值類型中間的 泛型標(biāo)識(shí)符 <T,E,…>,是 泛型方法的標(biāo)志,只有這種格式聲明的方法才是泛型方法。
泛型方法聲明時(shí)的 泛型標(biāo)識(shí)符 <T,E,…> 表示在方法可以使用聲明的泛型類型。
與泛型類相同,泛型標(biāo)識(shí)符可以是任意類型,常見的如T,E,K,V 等。
泛型方法可以聲明為 static 的,并且與普通的靜態(tài)方法是一樣的。
2.泛型方法的使用
2.1 普通泛型方法
- 聲明
/** * author : northcastle * createTime:2021/10/23 * 泛型方法的定義 */ public class GenericMethod { //1.普通的泛型方法 public <T> String commonMethod(String name,T t){ String res = ""; res += name +"-"+ t; System.out.println("普通泛型方法 : "+res); return res; } }
- 調(diào)用
/** * author : northcastle * createTime:2021/10/23 */ public class GenericMethodApplication { public static void main(String[] args) { //1.調(diào)用普通泛型方法 GenericMethod genericMethod = new GenericMethod(); String commonRes01 = genericMethod.commonMethod("001", "bb"); System.out.println(commonRes01); String commonRes02 = genericMethod.commonMethod("002", 100); System.out.println(commonRes02); String commonRes03 = genericMethod.commonMethod("003", true); System.out.println(commonRes03); System.out.println("=================="); } }
- 運(yùn)行結(jié)果
普通泛型方法 : 001-bb
001-bb
普通泛型方法 : 002-100
002-100
普通泛型方法 : 003-true
003-true
==================
2.2 靜態(tài)泛型方法
- 聲明
/** * author : northcastle * createTime:2021/10/23 * 泛型方法的定義 */ public class GenericMethod { //2.靜態(tài)的泛型方法 public static <T,E> String staticMethod(String name,T t,E e){ String res = ""; res += name +"-"+ t +"-"+ e; System.out.println("靜態(tài)泛型方法 : "+res); return res; } }
- 調(diào)用
package com.northcastle.genericmethod; /** * author : northcastle * createTime:2021/10/23 */ public class GenericMethodApplication { public static void main(String[] args) { //2.調(diào)用靜態(tài)泛型方法 String staticRes01 = GenericMethod.staticMethod("001", "aa", "bb"); System.out.println(staticRes01); String staticRes02 = GenericMethod.staticMethod("002", 100, 'c'); System.out.println(staticRes02); String staticRes03 = GenericMethod.staticMethod("003", 12.05d, false); System.out.println(staticRes03); System.out.println("=================="); } }
- 運(yùn)行結(jié)果
靜態(tài)泛型方法 : 001-aa-bb
001-aa-bb
靜態(tài)泛型方法 : 002-100-c
002-100-c
靜態(tài)泛型方法 : 003-12.05-false
003-12.05-false
==================
2.3 泛型方法中的可變參數(shù)
- 聲明
/** * author : northcastle * createTime:2021/10/23 * 泛型方法的定義 */ public class GenericMethod { //3.帶可變參數(shù)的泛型方法 public <A> void argsMethod(A ... args){ for (A arg : args) { System.out.println(arg); } } }
- 調(diào)用
package com.northcastle.genericmethod; /** * author : northcastle * createTime:2021/10/23 */ public class GenericMethodApplication { public static void main(String[] args) { //3.調(diào)用可變參數(shù)的方法 genericMethod.argsMethod(1,2,300,400,500,600); System.out.println("=================="); } }
- 運(yùn)行結(jié)果
1
2
300
400
500
600
==================
完成~
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot中自定義異常以及定制異常界面實(shí)現(xiàn)過程解析
這篇文章主要介紹了springboot中自定義異常以及定制異常界面實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09使用 Redis 緩存實(shí)現(xiàn)點(diǎn)贊和取消點(diǎn)贊的示例代碼
這篇文章主要介紹了使用 Redis 緩存實(shí)現(xiàn)點(diǎn)贊和取消點(diǎn)贊的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Java Lock鎖多線程中實(shí)現(xiàn)流水線任務(wù)
這篇文章主要介紹了Java Lock鎖多線程中實(shí)現(xiàn)流水線任務(wù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05SpringBoot內(nèi)存數(shù)據(jù)導(dǎo)出成Excel的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于SpringBoot內(nèi)存數(shù)據(jù)導(dǎo)出成Excel的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12java父子線程之間實(shí)現(xiàn)共享傳遞數(shù)據(jù)
本文介紹了Java中父子線程間共享傳遞數(shù)據(jù)的幾種方法,包括ThreadLocal變量、并發(fā)集合和內(nèi)存隊(duì)列或消息隊(duì)列,并提醒注意并發(fā)安全問題2025-02-02調(diào)用Mybatis?plus中的saveBatch方法報(bào)找不到表的問題
在用Mybatis plus開發(fā)的項(xiàng)目中,用自帶的API批量保存的方法saveBatch操作時(shí),發(fā)現(xiàn)報(bào)沒有找到表的錯(cuò)誤,本文就來詳細(xì)的介紹一下解決方法,感興趣的可以了解一下2024-03-03