前言:
https://www.cnblogs.com/LoveBB/p/17277662.html
什么是范型
JDK 1.5開始引入Java泛型(generics)這個特性,該特性提供了編譯時型別安全檢測機制,允許程式員在編譯時檢測到非法的型別,停,業余的客套話就不多說了,這些術語,講了N遍,不管你看不看得懂,我的目的就是就要把你教懂,所以,這里不是重點,
泛型的作用
泛型有四個作用:型別安全、自動轉換、性能提升、可復用性,即在編譯的時候檢查型別安全,將所有的強制轉換都自動和隱式進行,同時提高代碼的可復用性,實在不想說,但是不說又不行,生怕你們把路走偏了,
泛型的使用
- 我們寫代碼,基本上就是:類、介面、方法,
泛型類
- 先來來個3簡單的類,平時的類動則幾十上百行,現在我們化繁為簡,就是為了讓我們更清楚類的結構,
正經類
public class Student {
}
public class Teacher {
}
public class Room {
}
有些人心里忍不住要噴了,這誰不會呀,嘿嘿,別著急,這想說的就是我們修煉的名門正派功法,這是很標準的"正派"寫法,接下來,我將使用魔教功法進行魔改,
范型類結構
public class 類名 <泛型型別1,...> {
// todo
}
范型類
- 一個范型
簡簡單單
public class Student<T> {
}
- 兩個范型
我好像在罵人,但是你沒有證據吧
public class Student<S,B> {
}
- 三個范型
這里注意,范型里面的 S, B, Q 都是自定義的,一般用大寫字母表示,個數不限
public class Student<S, B, Q> {
}
- N 個范型
嘿嘿,個數不限,那就來個不限的吧
public class Student<S, B, Q, V, N, F, D, A, U, I, O, P, L, H, J, K, G> {
}
- 關于字母
有人說,字母就26個啊,你寫重復的話,那不是一樣了嘛,對,你是對的,但是魔教功法就在于,他不講道理,誰說一定要使用大寫字母了,下面來個逼死強迫癥寫法,什么叫無限火力,放飛自我,
public class Student<Sq, VIP, diss, QR, LKl, WOrd, Hao, Da, VPP, Ji, Ni, Tai, Mei, N, WoCao, D, CPDD, U, I, Love, Dog, OPHJKG, JiuK, HangZhou, WO, LAi, CoffEr, VCS> {
}
怎么樣,好好的一個類,經過魔教功法的改造,是不是看起來很不一樣了,
- 一般規定
雖然我們可以亂寫,但是魔教功法還是有一套自己的體系,一般關于大寫字母的定義如下
T:任意型別 type
E:集合中元素的型別 element
K:key-value形式 key
V: key-value形式 value
N: Number(數值型別)
?: 表示不確定的java型別
對比
- 為了加深記憶,我們前后再對比一下內容
// 改造前
public class Student {
}
// 改造后
public class Student<T> {
}
實體化
- 范型類應該怎么樣實體化呢,請看下面正派寫法
// 正經寫法
Student student = new Student();
// 范型寫法
Student<T> student = new Student<>();
// 案例1
Student<String> student = new Student<>();
// 案例2
Student<Integer> student = new Student<>();
// 案例3
Student<Teacher> student = new Student<>();
// 案例4:前面都沒意思,來試試套娃吧,哈哈,走火入魔了沒有,魔功就是魔功,這里要自己動手,才清晰明了
Student<Student<Student<Student<Student<String, Student<String, Long>>, Boolean>, Integer>, String>, Student<String, Integer>> student = new Student<>();
// 案例5:那個最長的,我實在懶得寫了,后面全部用String代替了,這一套寫下來,能不能唬住人,
Student<
String,
BigDecimal,
Integer,
Boolean,
Long,
Double,
Room,
Teacher,
String,
String,
String,
String,
String,
String,
String,
String,
String
> student = new Student<>();
咱就是說,用了魔教功法之后,我們 new 出來的物件,想傳什么就傳什么,有多少個范型引數,就傳多少個物件進去,總之就一句話,很強,運用好了,絕對是秒天秒地秒空氣,crud 之內,已然無敵,
泛型介面
看過范型類了,那接下來就是介面,有一說一,范型介面的使用率比范型類高出很多很多
正經介面
public interface Student {
// todo
}
范型介面結構
public interface 介面名<T> {
// todo
}
范型介面
- 兩個引數
public interface Student<T> {
// todo
}
- 兩個引數
public interface Student<S, B> {
// todo
}
- 三個引數
停,就到這里吧,再寫下去就不禮貌了,容易走火入魔,引數是和類一樣的,可以N個,英文字母不限大小,
對比
// 改造前
public interface Student {
// todo
}
// 改造后
public interface Student<T> {
// todo
}
- 實體化
眾所周知,介面只能用實作介面的方式來實體化,java8之后還能用函式式編程,這里就不展開來講了
// 正經實作
public class GoodStudent implements Student{
}
// 范型實作
public class GoodStudent<T> implements Student<T>{
}
// 這里不用 T 可不可以呢,答案是可以的,只要子類和父類的范型一樣就行
public class GoodStudent<B> implements Student<B>{
}
// 實戰1
public class GoodStudent<String> implements Student<String>{
}
// 實戰2
public class GoodStudent<Integer> implements Student<Integer>{
}
// 實戰N,別忘記了我們還能套娃,還能無限火力N,這里就不演示了
- 那子類和父類的范型不一樣呢,那就編譯器報錯唄

使用方式
那說了那么多,這個范型有什么用呢,答案是:介面、類宣告中定義的型別形參則可以在整個介面、類中使用,
可以作為引數型別,入參,回傳值使用
- 范型使用
public class GoodStudent<T> implements Student<T>{
// 范型作為引數型別
private T personality;
// 范型介面1,作為入參
public void evaluation(T t){
}
// 范型介面2,作為入參和回傳值
public T evaluation2(T t){
return t;
}
}
- 案例參考(比較容易看懂)
// 實體化后很簡單的,一看就懂,這就是我們正常的寫法
public class GoodStudent<String> implements Student<String>{
// 范型引數
private String personality;
public void evaluation(String t){
}
public String evaluation2(String t){
return t;
}
}
// 案例2
public class GoodStudent<Integer> implements Student<Integer>{
// 范型引數
private Integer personality;
public void evaluation(Integer t){
}
public Integer evaluation2(Integer t){
return t;
}
}
- 實體化
public static void main(String[] args) {
// String 型別
GoodStudent<String> goodStudent = new GoodStudent<>();
String personality = "";
goodStudent.evaluation(personality);
String result = goodStudent.evaluation2(personality);
// Integer 型別
GoodStudent<Integer> goodStudent = new GoodStudent<>();
Integer personality = "";
goodStudent.evaluation(personality);
Integer result = goodStudent.evaluation2(personality);
// 套娃型別,我就套一層,不然容易走火入魔
GoodStudent<GoodStudent<String>> goodStudent = new GoodStudent<>();
GoodStudent<String> personality = new GoodStudent<>();
// 內層
String resultString = personality.evaluation2("");
goodStudent.evaluation(personality);
// 外層
GoodStudent<String> result = goodStudent.evaluation2(personality);
}
泛型方法
- 介紹范型方法之前,我們先看看普通方法,我們經常寫的方法就是這樣
正經方法
// 無回傳值,無入參
public void test(){
}
// 無回傳值,有入參
public void test(String s){
}
// 有回傳值,無入參
public String test(){
return "";
}
// 有回傳值,有入參
public String test(String s){
return s;
}
范型方法結構
// 范型方法就是加上一個范型宣告
public <泛型型別> 回傳型別 方法名(泛型型別 變數名) {
// todo
}
范型演示(注意和之前正常的對比)
// 無回傳值,無入參(無意義)
public <T> void test(){
}
// 無回傳值,有入參(不常用)
public <T> void test(T s){
}
// 有回傳值,無入參(不太常用)
public <T> T test(){
retrn null;
}
// 有回傳值,有入參(經常用)
public <T> T test(T s){
return s;
}
案例
- 正經案例
public class GoodStudent implements Student {
// 無回傳值,無入參
public void test() {
}
// 無回傳值,有入參
public void test2(String s) {
}
// 有回傳值,無入參
public String test3() {
return "";
}
// 有回傳值,有入參
public String test4(String s) {
return s;
}
}
- 正經呼叫
public static void main(String[] args) {
GoodStudent goodStudent = new GoodStudent();
goodStudent.test();
String s = "";
goodStudent.test2(s);
String s1 = goodStudent.test3();
String s2 = goodStudent.test4(s);
}
- 范型案例
public class GoodStudent implements Student {
// 無回傳值,無入參(無意義)
public <T> void test(){
}
// 無回傳值,有入參(不常用)
public <T> void test2(T s){
}
// 有回傳值,無入參(不太常用)
public <T> T test3(){
return null;
}
// 有回傳值,有入參(經常用)
public <T> T test4(T s){
return s;
}
}
- 范型呼叫
public static void main(String[] args) {
// String 范型
GoodStudent goodStudent = new GoodStudent();
goodStudent.test();
String s = "";
goodStudent.test2(s);
String s1 = goodStudent.test3();
String s2 = goodStudent.test4(s);
// Integer 范型
GoodStudent goodStudent = new GoodStudent();
goodStudent.test();
Integer s = "";
goodStudent.test2(s);
Integer s1 = goodStudent.test3();
Integer s2 = goodStudent.test4(s);
// Student<String> 范型
GoodStudent goodStudent = new GoodStudent<>();
goodStudent.test();
Student<String> s = new GoodStudent<>();
goodStudent.test2(s);
Student<String> s1 = goodStudent.test3();
Student<String> s2 = goodStudent.test4(s);
}
范型方法是傳什么引數,就是什么回傳值
泛型通配符(上下界)
你以為這就完了?下面才是絕殺,
Java泛型的通配符是用于解決泛型之間參考傳遞問題的特殊語法, 主要有以下三類:
- 無邊界的通配符,使用精確的引數型別
- 關鍵字宣告了型別的上界,表示引數化的型別可能是所指定的型別,或者是此型別的子類
- 關鍵字宣告了型別的下界,表示引數化的型別可能是指定的型別,或者是此型別的父類
無邊界更多是服務于上界和下界的
// 表示型別引數可以是任何型別
public class B<?> {
}
// 上界:表示型別引數必須是A或者是A的子類
public class B<T extends A> {
}
// 下界:表示型別引數必須是A或者是A的超型別
public class B<T supers A> {
}
正常類
public class GoodStudent implements Student{
// String引數
private String personality;
// String 作為入參
public void evaluation(String t){
}
// String作為入參和回傳值
public String evaluation2(String t){
return t;
}
}
- 范型類
public class GoodStudent<T> implements Student<T>{
// 范型引數
private T personality;
// 范型介面1,作為入參
public void evaluation(T t){
}
// 范型介面2,作為入參和回傳值
public T evaluation2(T t){
return t;
}
}
- 下界范型
public class GoodStudent<T extends String> implements Student<T> {
// 范型引數
private T personality;
// 范型介面1,作為入參
public void evaluation(T t){
}
// 范型介面2,作為入參和回傳值
public T evaluation2(T t){
return t;
}
}
到這里,我已經走火入魔了,后續的后面在寫了,從這里我們也能看出,一個“正派”的類,經過”魔教“功法范型的改造之后,已經具備一定的復雜性了,這就是 魔功-范型 帶來的威力,
// 改造前
public class GoodStudent implements Student{
// String引數
private String personality;
// String 作為入參
public void evaluation(String t){
}
// String作為入參和回傳值
public String evaluation2(String t){
return t;
}
}
// 改造后
public class GoodStudent<T extends String> implements Student<T> {
// 范型引數
private T personality;
// 范型介面1,作為入參
public void evaluation(T t){
}
// 范型介面2,作為入參和回傳值
public T evaluation2(T t){
return t;
}
}
作者:天下沒有收費的bug
出處:https://www.cnblogs.com/LoveBB/
本文著作權歸作者和博客園共有,歡迎轉載,但未經作者同意必須在文章頁面給出原文鏈接,否則保留追究法律責任的權利,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/548768.html
標籤:Java
下一篇:SpringBoot高頻面試題
