泛型简单使用:
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 }
效果截图: