泛型簡單使用:
1 package com.etc; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 /* 6 泛型就相當於<>一個標簽,泛化類型,一般用於規定集合的數據存放類型。 7 instanceof方法是用來判斷某個對象是否為某一個類的實例 8 9 */ 10 public class Test { 11 12 public static void main(String[] args) { 13 14 List<String> list=new ArrayList<String>(); 15 list.add("abc"); 16 //無法存放除String類型的數據,較為安全,會進行類型檢查 17 //list.add(1); 18 //取數據較為省心 19 System.out.println(list.get(0)); 20 21 } 22 23 }
1.泛型類:
1 package com.test; 2 /* 3 自定義泛型類的使用,盡量用大寫字母表示 4 T -> Type 5 K V ->Key Value 6 盡量見名知其意 7 泛型不能用於靜態的屬性 8 */ 9 public class Teacher<T> { 10 11 public T name; 12 // private T id; 13 14 public Teacher(T name) { 15 super(); 16 this.name = name; 17 } 18 19 public Teacher() { 20 super(); 21 } 22 23 public T getName() { 24 return name; 25 } 26 public void setName(T name) { 27 this.name = name; 28 } 29 30 31 }
泛型類的使用:
1 package com.test; 2 /* 3 自定義泛型類的使用,在聲明時需要指定具體的類型,不能為基本類型 4 */ 5 public class TestTeacher { 6 7 public static void main(String[] args) { 8 9 Teacher<String> list=new Teacher<String>(); 10 list.setName("皮卡丘"); 11 System.out.println(list.getName()); 12 13 } 14 15 }
效果截圖:
2.泛型接口:
1 package com.test; 2 3 public interface Person <T>{ 4 //泛型不能用於全局變量前 5 /*public static final 編譯時自動添加*/ 6 int MAX_VALUE=1; 7 /*public abstract 編譯時自動添加*/ 8 T compare(T t); 9 10 }
泛型接口的實現:
1 package com.test; 2 //泛型接口的實現,必須指定泛型的類型 3 public class TestPerson implements Person<String> { 4 5 public static void main(String[] args) { 6 7 String t="I like codes very much!!!!"; 8 TestPerson ts=new TestPerson(); 9 System.out.println(ts.compare(t)); 10 11 } 12 13 @Override 14 public String compare(String t) { 15 return t; 16 } 17 18 }
效果截圖:
3.常用泛型形式:
(1)Student.java
1 package com.test; 2 3 import java.text.DateFormat; 4 import java.text.ParseException; 5 import java.text.SimpleDateFormat; 6 import java.util.Date; 7 8 public class Student { 9 10 public String name; 11 public int id; 12 public Date birth; 13 14 public Student(String name, int id, String birth) { 15 super(); 16 this.name = name; 17 this.id = id; 18 DateFormat format=new SimpleDateFormat("yyyy-MM"); 19 try { 20 this.birth = format.parse(birth); 21 } catch (ParseException e) { 22 e.printStackTrace(); 23 } 24 } 25 public String getName() { 26 return name; 27 } 28 public void setName(String name) { 29 this.name = name; 30 } 31 public int getId() { 32 return id; 33 } 34 public void setId(int id) { 35 this.id = id; 36 } 37 public Date getBirth() { 38 return birth; 39 } 40 public void setBirth(Date birth) { 41 this.birth = birth; 42 } 43 44 45 46 }
(2)TestStudent.java
1 package com.test; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class TestStudent { 7 8 public static void main(String[] args) { 9 10 List<Student> list=new ArrayList<Student>(); 11 Student stu1=new Student("張三",1,"1998-03"); 12 Student stu2=new Student("李四",2,"1998-04"); 13 list.add(stu1); 14 list.add(stu2); 15 System.out.println(list.get(0).getName()+" "+list.get(0).getId()+" "+list.get(0).getBirth()); 16 System.out.println(list.get(1).getName()+" "+list.get(1).getId()+" "+list.get(1).getBirth()); 17 18 } 19 20 }
效果截圖: