泛型之前
在面向对象编程语言中,多态算是一种泛化机制。例如,你可以将方法的参数类型设置为基类,那么该方法就可以接受从这个基类中导出的任何类
作为参数,这样的方法将会更具有通用性。此外,如果将方法参数声明为接口,将会更加灵活。
在Java增加泛型类型之前,通用程序的设计就是利用继承实现的,例如,ArrayList类只维护一个Object引用的数组,Object为所有类基类。
代码示例
public class BeforeGeneric { static class ArrayList{//泛型之前的通用程序设计 private Object[] elements=new Object[0]; public Object get(int i){ return elements[i]; } public void add(Object o){ //这里的实现,只是为了演示,不具有任何参考价值 int length=elements.length; Object[] newElments=new Object[length+1]; for(int i=0;i<length;i++){ newElments[i]=elements[i]; } newElments[length]=o; elements=newElments; } } public static void main(String[] args) { ArrayList stringValues=new ArrayList(); stringValues.add(1);//可以向数组中添加任何类型的对象 //问题1——获取值时必须强制转换 String str=(String) stringValues.get(0); //问题2——上述强制转型编译时不会出错,而运行时报异常java.lang.ClassCastException } }
问题:使用ArrayList集合存储元素遍历的时候,按照正常的操作出现了问题,
当前ArrayList集合中存储了两种类型的元素分别String和Integer类型,在遍历的时候,是用String接收的,对于
Integer类型就出现了异常!
回想数组:String[] str = new String[3] ;
str[0] = "hello" ;
str[1] = "world" ;
str[2] = 100 ; 错误的,
数组直接定义了存储的类型,防止出现其他类型的元素,集合能不能也像数组一样,直接规定我们集合的存储类型,针对这种情况
一种技术:泛型<数据类型>
泛型:将集合类型确定的工作推迟到了创建对象或者调用方法的时候,属于一种参数化类型,可以作为参数传递.
泛型的好处:1)将运行时期异常提前到了编译时期
2)优化了设计,解决了黄色警告线问题
3)避免了强制类型转换
泛型的引出可以提高程序的安全性!
public class GenericDemo { public static void main(String[] args) { //创建集合对象 ArrayList<String> array = new ArrayList<String>() ; //添加元素 array.add("hello") ; array.add("world") ; array.add("java") ; // array.add(new Integer(100)) ; //已经定了当前集合时String类型 //遍历 //获取迭代器 Iterator<String> it = array.iterator() ; while(it.hasNext()) { //java.lang.ClassCastException:类转换异常 String s = it.next() ; System.out.println(s); } } }
使用ArrayList集合存储自定义对象并遍历,加入泛型
代码示例
package org.westos_01; /** * @author Administrator * */ public class Student { private String name; private int age; public Student() { super(); } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
public class GenericDemo2 { public static void main(String[] args) { //创建集合对象 ArrayList<Student> array = new ArrayList<Student>() ; //=号右边的泛型:泛型推断 //创建学生对象 Student s1 = new Student("王昭君",25) ; Student s2 = new Student("西施",27) ; Student s3 = new Student("杨贵妃",25) ; Student s4 = new Student("貂蝉",28) ; //添加到集合中 array.add(s1) ; array.add(s2) ; array.add(s3) ; array.add(s4) ; //获取迭代器 Iterator<Student> it = array.iterator() ; while(it.hasNext()) { Student s = it.next() ; System.out.println(s.getName()+"----"+s.getAge()); } System.out.println("------------------"); //普通for循环 for(int x = 0 ; x < array.size() ; x ++) { Student s = array.get(x) ; System.out.println(s.getName()+"---"+s.getAge()); } } }
package org.westos.h_objectarray; public class Student { private String name; private int age; public Student() { super(); } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } }
package org.westos.h_objectarray; /** * 对象数组:可以存储对象的数组 需求:我有5个学生,5个学生有自己的姓名,年龄,遍历当前学生数组,获取到每一个学生的信息 * * 1)自定义类:Student name,age 2)在测试类中:创建一个数组,可以存储Stduent类型的 3)根据的提供长度,分别创建5个具体的学生对象 * 4)赋值并且遍历 * */ public class ObjectArrayDemo { public static void main(String[] args) { // 创建一个数组,可以存储Student类型 (对象数组) // 创建数组的格式:int[] arr= new int[长度] ; Student[] students = new Student[5]; // 需要创建5个学生对象 Student s1 = new Student("高圆圆", 27); Student s2 = new Student("王力宏", 30); Student s3 = new Student("唐嫣", 29); Student s4 = new Student("克劳泽", 35); Student s5 = new Student("拉姆", 36); // 赋值 students[0] = s1; students[1] = s2; students[2] = s3; students[3] = s4; students[4] = s5; // 遍历学生数组 for (int x = 0; x < students.length; x++) { // System.out.println(students[x]); Student s = students[x]; System.out.println(s.getName() + "----" + s.getAge()); } } }
ObjectTool
代码示例
package org.westos; public class ObjectTool { public Object object; public ObjectTool() { super(); // TODO Auto-generated constructor stub } public ObjectTool(Object object) { super(); this.object = object; } public Object getObject() { return object; } public void setObject(Object object) { this.object = object; } }
package org.westos; public class ObjectDemo { public static void main(String[] args) { // TODO Auto-generated method stub ObjectTool object = new ObjectTool(); object.setObject(new String("高圆圆")); // Object obj=new String("高圆圆") String string = (String) object.getObject(); // 向下转型 System.out.println("姓名是:" + string); object.setObject(new Integer(27)); Integer integer = (Integer) object.getObject();// Object System.out.println("年龄:" + integer); // java.lang.ClassCastException :程序不安全 :类转换异常 (转换不当) // object.setObject(new String("高圆圆")); // Integer integer2 = (Integer) object.getObject(); // //将String类型转换为Integer类型将抛出类转换异常 // System.out.println(integer); } }