高效能網站架構方案(三) ——Varnish加速與Gearman任務分發
阿新 • • 發佈:2022-05-03
泛型
泛型概述
泛型:是JDK5中引入的特性,它提供了編譯時型別安全檢測機制,該機制允許在編譯時檢測到非法的型別
它的本質是引數化型別,也就是說所操作的資料型別被指定為一個引數
一提到引數,最熟悉的就是定義方法時有形參,然後呼叫此方法時傳遞實參。那麼引數化型別如何理解?
顧名思義,就是將型別有原來的具體的型別引數化,然後在使用/呼叫時傳入具體的型別
這種引數型別可以用在類、方法和介面中,分別被稱為泛型類、泛型方法、泛型介面
泛型定義格式:
-
- <型別>: 指定一種型別的格式。這裡的型別可以看成是形參
- <型別1,型別2…> 指定多種型別的格式,多種型別之間用逗號隔開。這裡的型別可以看成是形參
- 將來具體呼叫時候給定的型別可以看成是實參,並且實參的型別只能是引用資料型別
泛型的好處:
-
- 把執行時問題提前到了編譯期間
- 避免了強制型別轉換
1 import java.util.ArrayList; 2 import java.util.Collection; 3 import java.util.Iterator; 4 5 /* 6 泛型 7 8 需求:Collection集合儲存字串並遍歷 9 */ 10 public class GenericDemo { 11 public static void main(String[] args) {12 //建立集合物件 13 // Collection c=new ArrayList(); 14 Collection<String> c = new ArrayList<String>(); 15 16 //新增元素 17 c.add("hello"); 18 c.add("world"); 19 c.add("java"); 20 // c.add(100);//執行時期問題改為編譯期問題 21 22 //遍歷集合 23 // Iterator it = c.iterator();24 Iterator<String> it = c.iterator(); 25 while (it.hasNext()) { 26 // Object obj = it.next(); 27 // System.out.println(obj); 28 29 //向下轉型 30 // String s=(String)it.next();//執行時問題:ClassCastException 31 String s = it.next();//不需要強制型別轉換 32 System.out.println(s); 33 } 34 35 }
泛型類
- 格式:修飾符 class 類名<型別> {}
<>中表示泛型的引數可以是任意標識,常見的如T、E、K、V等
1 //學生類 2 public class Student { 3 private String name; 4 5 public String getName() { 6 return name; 7 } 8 9 public void setName(String name) { 10 this.name = name; 11 } 12 13 }
1 //老師類 2 public class Teacher { 3 private Integer age; 4 5 public Integer getAge() { 6 return age; 7 } 8 9 public void setAge(Integer age) { 10 this.age = age; 11 } 12 13 }
1 /* 2 泛型類的定義格式: 3 4 - 格式:修飾符 class 類名<型別>{} 5 - 範例:public class Generic<T>{} 6 此處**T**可以隨便寫為任意標識,常見的如**T、E、K、V**等形式的引數常用於表示泛型 7 */ 8 9 public class Generic<T> { 10 private T t; 11 12 public T getT() { 13 return t; 14 } 15 16 public void setT(T t) { 17 this.t = t; 18 } 19 20 }
1 //測試類 2 public class GenericDemo { 3 public static void main(String[] args) { 4 Student s = new Student(); 5 s.setName("小白"); 6 System.out.println(s.getName()); 7 8 Teacher t = new Teacher(); 9 t.setAge(23); 10 // t.setAge("34");//報錯 11 System.out.println(t.getAge()); 12 System.out.println("--------"); 13 14 Generic<String> g1 = new Generic<String>(); 15 g1.setT("小黑"); 16 System.out.println(g1.getT()); 17 18 Generic<Integer> g2 = new Generic<Integer>(); 19 g2.setT(12); 20 System.out.println(g2.getT()); 21 22 Generic<Boolean> g3 = new Generic<Boolean>(); 23 g3.setT(true); 24 System.out.println(g3.getT()); 25 26 } 27 }
泛型方法
- 格式:修飾符 <型別> 返回值型別 方法名(型別 變數名) { }
1 /*泛型方法改進*/ 2 public class Generic { 3 public <T> void show(T t) { 4 System.out.println(t); 5 } 6 } 7 8 9 public class GenericDemo2 { 10 public static void main(String[] args) { 11 12 Generic g = new Generic(); 13 g.show("小白"); 14 g.show(24); 15 g.show(true); 16 g.show(12.34); 17 18 } 19 }
泛型介面
- 格式:修飾符 interface 介面名<型別> { }
1 //泛型介面 2 public interface Generic1<T> { 3 void show(T t); 4 }
1 //泛型類繼承介面 2 public class Genriclmpl<T> implements Generic1<T> { 3 @Override 4 public void show(T t) { 5 // TODO Auto-generated method stub 6 System.out.println(t); 7 } 8 }
1 //測試類 2 public class GenericDemo3 { 3 public static void main(String[] args) { 4 Generic1<String> g1 = new Genriclmpl<String>(); 5 g1.show("小白"); 6 7 Generic1<Integer> g2 = new Genriclmpl<Integer>(); 8 g2.show(23); 9 10 Generic1<Boolean> g3 = new Genriclmpl<Boolean>(); 11 g3.show(true); 12 13 } 14 }
型別萬用字元:<?>
- 使用型別萬用字元,可以用於表示各種泛型List的父類
- 若希望它代表某一類泛型List的父類,可以使用型別萬用字元的上限
- 若希望它代表某一類泛型List的子類,可以使用型別萬用字元的下限
1 import java.util.ArrayList; 2 import java.util.List; 3 4 /* 5 **型別萬用字元** 6 7 為了表示各種泛型List的父類,可以使用型別萬用字元 8 9 - 型別萬用字元:**<?>** 10 - List<?>:表示元素型別未知的List,它的元素可以匹配**任何的型別** 11 - 這種帶萬用字元的List僅表示它是各種泛型List的父類,並不能把元素新增到其中 12 13 如果說我們不希望List<?>是任何泛型List的父類,只希望它代表某一類泛型List的父類,可以使用型別萬用字元的上限 14 15 - 型別萬用字元上限:**<? extends 型別>** 16 - List<? extends Number>:它表示的型別是**Number或者其子型別** 17 18 除了可以指定型別萬用字元的上限,我們也可以指定型別萬用字元的下限 19 20 - 型別萬用字元下限:**<? super 型別>** 21 - List<? super Number>:它表示的型別是**Number或者其父型別** 22 */ 23 24 public class GenericDemo4 { 25 public static void main(String[] args) { 26 //型別萬用字元:<?> 27 List<?> list1 = new ArrayList<Object>(); 28 List<?> list2 = new ArrayList<Number>(); 29 List<?> list3 = new ArrayList<Integer>(); 30 System.out.println("--------"); 31 32 // 型別萬用字元上限:<? extends 型別> 33 // List<? extends Number> list4=new ArrayList<Object>();//報錯 34 List<? extends Number> list5 = new ArrayList<Number>(); 35 List<? extends Number> list6 = new ArrayList<Integer>(); 36 System.out.println("--------"); 37 38 //型別萬用字元下限:<? super 型別> 39 List<? super Number> list7 = new ArrayList<Object>(); 40 List<? super Number> list8 = new ArrayList<Number>(); 41 // List<? super Number> list9=new ArrayList<Integer>();//報錯 42 43 } 44 }
可變引數
可變引數又稱引數個數可變,用作方法的形參出現,那麼方法引數個數就是可變的了
-
- 格式:修飾符 返回值型別 方法名(資料型別… 變數名) { }
- 範例:public static int sum(int… a) { }
可變引數注意事項:
-
- 這裡的變數其實是一個數組
- 如果一個方法有多個引數,包含可變引數,可變引數要放在最後
1 public class ArgsDemo { 2 public static void main(String[] args) { 3 System.out.println(sum(1, 2)); 4 System.out.println(sum(1, 2, 3)); 5 System.out.println(sum(1, 2, 3, 4)); 6 7 System.out.println(sum(1, 2, 3, 4, 5)); 8 System.out.println(sum(1, 2, 3, 4, 5, 6)); 9 System.out.println(sum(1, 2, 3, 4, 5, 6, 7)); 10 11 } 12 13 // public static int sum(int... a,int b) {//報錯 14 // return 0; 15 // } 16 17 // public static int sum(int b,int... a) {//可變引數要放在後面 18 // return 0; 19 // } 20 21 public static int sum(int... a) { 22 // System.out.println(a); 23 // return 0; 24 int sum = 0; 25 for (int i : a) { 26 sum += i; 27 } 28 return sum; 29 30 } 31 32 // public static int sum(int a,int b) { 33 // return a+b; 34 // } 35 // public static int sum(int a,int b,int c) { 36 // return a+b+c; 37 // } 38 // public static int sum(int a,int b,int c,int d) { 39 // return a+b+c+d; 40 // } 41 }
可變引數的使用
1 import java.util.Arrays; 2 import java.util.List; 3 import java.util.Set; 4 5 /* 6 Arrays工具類中有一個靜態方法: 7 8 - public static <T> List<T> asList(T... a):返回由指定陣列支援的固定大小的列表 9 10 List介面中有一個靜態方法: 11 12 - public static <E> List<E> of(E... elements):返回包含任意數量元素的不可變列表 13 14 Set介面中有一個靜態方法: 15 16 - public static <E> Set<E> of(E... elements):返回包含任意數量元素的不可變集合 17 */ 18 19 public class ArgsDemo2 { 20 public static void main(String[] args) { 21 22 //public static <T> List<T> asList(T... a):返回由指定陣列支援的固定大小的列表 23 List<String> list = Arrays.asList("hello","world","java"); 24 25 // list.add("javaee");//UnsupportedOperationException:不支援請求操作 26 // list.remove("world");//UnsupportedOperationException 27 list.set(1,"javaee");//新增和刪除都會修改List的大小,所以不可以用,修改不會改變大小,可以用 28 System.out.println(list); 29 30 31 32 //public static <E> List<E> of(E... elements):返回包含任意數量元素的不可變列表 33 List<String> list = List.of("hello","world","java","world"); 34 35 // list.add("javaee");//UnsupportedOperationException 36 // list.remove("world");//UnsupportedOperationException 37 // list.set(1,"javaee");//UnsupportedOperationException 38 //增刪改都不可以 39 System.out.println(list); 40 41 42 43 //public static <E> Set<E> of(E... elements):返回包含任意數量元素的不可變集合 44 Set<String> set = Set.of("hello","world","java"); 45 // Set<String> set = Set.of("hello","world","java","world");//IllegalArgumentException:非法或不正確的引數 46 //因為set不能有重複元素 47 48 // set.add("javaee");//UnsupportedOperationException 49 // set.remove("world");//UnsupportedOperationException 50 System.out.println(set); 51 52 } 53 }