首页 > 代码库 > Java泛型学习
Java泛型学习
1、泛型的概念
泛型即“参数化类型”,就比如我们定义方法的时候,定义一个变量,称为形参,变量值根据传进去的实参的值不同而改变。而泛型的出现,就是为了解决类型也能根据传进去的类型改变的问题,所以称为参数化类型,也就是说所操作的数据类型被指定为一个参数。主要用在定义方法、类、集合上,可以很好的减少代码的重复。
2、泛型中的标识符含义
E - Element (在集合中使用,因为集合中存放的是元素)
T - Type(Java 类)
K - Key(键)
V - Value(值)
N - Number(数值类型)
? - 表示不确定的java类型
S、U、V - 2nd、3rd、4th types
3、定义一个泛型方法
首先,泛型的声明,必须在方法的修饰符(public,static,final,abstract等)之后,返回值声明之前。然后,和泛型类一样,可以声明多个泛型,用逗号隔开
1 import java.util.ArrayList; 2 import java.util.List; 3 4 /** 5 * Created by Administrator on 2017/6/5. 6 * <p> 7 * Description: 8 */ 9 public class One 10 { 11 public static void main(String[] args) 12 { 13 List<Integer> integers=new ArrayList<>(); 14 integers.add(111); 15 integers.add(222); 16 17 List<String> list=new ArrayList<>(); 18 list.add("111"); 19 list.add("222"); 20 21 System.out.println(print(integers,list)); 22 23 } 24 25 public static <T1,T2> T1 print(List<T1> list,List<T2> list2){ 26 T1 t1 = list.get(0); 27 return t1; 28 } 29 }
4、定义一个泛型类
1 /** 2 * Created by Administrator on 2017/6/5. 3 * <p> 4 * Description: 5 */ 6 public class Box<T>//这里可以定义多个泛型,用逗号分割 7 { 8 private String name; 9 private T t; 10 11 public Box() 12 { 13 14 } 15 16 public Box(T t, String name) 17 { 18 this.t = t; 19 this.name = name; 20 } 21 22 public String getName() 23 { 24 return name; 25 } 26 27 public void setName(String name) 28 { 29 this.name = name; 30 } 31 32 public T getT() 33 { 34 return t; 35 } 36 37 public void setT(T t) 38 { 39 this.t = t; 40 } 41 }
1 /** 2 * Created by Administrator on 2017/6/5. 3 * <p> 4 * Description: 5 */ 6 public class Two 7 { 8 public static void main(String[] args) 9 { 10 Box<String> box=new Box<>("stirng","小明"); 11 String t = box.getT(); 12 System.out.println(t); 13 14 Box<Integer> box1=new Box<>(123456,"小红"); 15 Integer t1 = box1.getT(); 16 System.out.println(t1); 17 } 18 }
5、泛型类的继承
父类:
1 package fan; 2 3 /** 4 * Created by Administrator on 2017/6/5. 5 * <p> 6 * Description: 7 */ 8 public class Person<T> 9 { 10 private String code; 11 private String name; 12 private T t; 13 14 public String getCode() 15 { 16 return code; 17 } 18 19 public void setCode(String code) 20 { 21 this.code = code; 22 } 23 24 public String getName() 25 { 26 return name; 27 } 28 29 public void setName(String name) 30 { 31 this.name = name; 32 } 33 34 public T getT() 35 { 36 return t; 37 } 38 39 public void setT(T t) 40 { 41 this.t = t; 42 } 43 44 @Override 45 public String toString() 46 { 47 return "Person{" + "code=‘" + code + ‘\‘‘ + ", name=‘" + name + ‘\‘‘ + ", t=" + t + ‘}‘; 48 } 49 }
两个子类
1 package fan; 2 3 /** 4 * Created by Administrator on 2017/6/5. 5 * <p> 6 * Description: 7 */ 8 public class Student extends Person<Integer> 9 { 10 }
package fan; /** * Created by Administrator on 2017/6/5. * <p> * Description: */ public class Teacher extends Person<String> { }
泛型辅助类
1 package fan; 2 3 /** 4 * Created by Administrator on 2017/6/5. 5 * <p> 6 * Description: 7 */ 8 public class BuildHelper<T extends Person>//这里的意思是对泛型进行限制,输入的泛型只能是继承Person类的 9 { 10 private Class<T> clazz; 11 12 public BuildHelper(){ 13 14 } 15 //构造器,仅限自己调用 16 private BuildHelper(Class<T> clazz){ 17 this.clazz=clazz; 18 } 19 20 public static <T extends Person> BuildHelper<T> with(Class<T> clazz){ 21 return new BuildHelper<T>(clazz); 22 } 23 24 //返回泛型类 25 public T build(){ 26 try { 27 T t = clazz.newInstance(); 28 return t; 29 } catch (InstantiationException e) { 30 e.printStackTrace(); 31 } catch (IllegalAccessException e) { 32 e.printStackTrace(); 33 } 34 return null; 35 } 36 }
测试
1 package fan; 2 3 /** 4 * Created by Administrator on 2017/6/5. 5 * <p> 6 * Description: 7 */ 8 public class Main 9 { 10 public static void main(String[] args) 11 { 12 BuildHelper<Student> studentBuildHelper = BuildHelper.with(Student.class); 13 Student student = studentBuildHelper.build(); 14 student.setT(123); 15 student.setCode("123"); 16 student.setName("123"); 17 18 BuildHelper<Teacher> teacherBuildHelper = BuildHelper.with(Teacher.class); 19 Teacher teacher = teacherBuildHelper.build(); 20 teacher.setT("1234"); 21 teacher.setCode("1234"); 22 teacher.setName("1234"); 23 24 System.out.println(student); 25 System.out.println(teacher); 26 } 27 }
Java泛型学习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。