1.为什么需要泛型
- java属于强类型编程语言,变量在使用之前,需要先进行定义,而地定义个变量是必须要指定其数据类型;
例如:声明一个类型为Object类型,然后向下转型就会报错;
public static void main(String[] args) throws Exception {
Circle circle = new Circle();
circle.setX(12);
circle.setY(9);
System.out.println(circle);
Circle circle2 = new Circle();
circle2.setX("圆心横坐标x");
circle2.setY("圆心纵坐标y");
System.out.println(circle2);
//Object转Double类型报错
Double x1 = (Double)circle2.getX();
Double y1 = (Double)circle2.getY();
System.out.println(circle2);
}
public static class Circle{
private Object x;
private Object y;
/**
* 获取x
* @return the x
*/
public Object getX() {
return x;
}
/**
* 设置x
* @param x the x to set
*/
public void setX(Object x) {
this.x = x;
}
/**
* 获取y
* @return the y
*/
public Object getY() {
return y;
}
/**
* 设置y
* @param y the y to set
*/
public void setY(Object y) {
this.y = y;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Circle{"+"x="+x+", y="+y+"}";
}
}
运行到向下转型就会报出错误
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
at main.main(main.java:21)
Circle{x=12, y=9}
Circle{x=圆心横坐标x, y=圆心纵坐标y}
2.泛型使用
- 泛型类型的定义方法为:类名后面尖括号<>
例如:定义类然后在使用时指定类型
public static void main(String[] args) throws Exception {
// Circle circle = new Circle();
// circle.setX(12);
// circle.setY(9);
// System.out.println(circle);
Circle<String,String> circle2 = new Circle<String,String>();
circle2.setX("圆心横坐标x");
circle2.setY("圆心纵坐标y");
System.out.println(circle2);
String x1 = (String)circle2.getX();
String y1 = (String)circle2.getY();
System.out.println(circle2);
}
public static class Circle<T1,T2>{
private T1 x;
private T2 y;
/**
* 获取x
* @return the x
*/
public T1 getX() {
return x;
}
/**
* 设置x
* @param x the x to set
*/
public void setX(T1 x) {
this.x = x;
}
/**
* 获取y
* @return the y
*/
public T2 getY() {
return y;
}
/**
* 设置y
* @param y the y to set
*/
public void setY(T2 y) {
this.y = y;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Circle{"+"x="+x+", y="+y+"}";
}
}
3.限制泛型类型的使用类型
- 我们需要对类型参数做一定的限制,只能传递部分参数类型传递其他类型则会报错,例如我们在前面的类型限制只能传递数字类型
例如:
public static void main(String[] args) throws Exception {
Circle<Integer,Integer> circle2 = new Circle<Integer,Integer>();
circle2.setX(12);
circle2.setY(13);
System.out.println(circle2);
}
public static class Circle<T1 extends Number,T2 extends Number>{
private T1 x;
private T2 y;
/**
* 获取x
* @return the x
*/
public T1 getX() {
return x;
}
/**
* 设置x
* @param x the x to set
*/
public void setX(T1 x) {
this.x = x;
}
/**
* 获取y
* @return the y
*/
public T2 getY() {
return y;
}
/**
* 设置y
* @param y the y to set
*/
public void setY(T2 y) {
this.y = y;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Circle{"+"x="+x+", y="+y+"}";
}
}
4.类型擦除
我们在使用泛型的过程中,如果没有指定数据类型,那么将会擦除泛型类型:
Circle circle2 = new Circle();
circle2.setX("圆心横坐标x");
circle2.setY("圆心纵坐标y");
System.out.println(circle2);
String x1 = (String)circle2.getX();
String y1 = (String)circle2.getY();
System.out.println(circle2);