/**
* @program: pro1
* @description 泛型只能引用类型,不能是基本类型,使用泛型避免了向下转型带来的安全隐患
* @author: ydcoding
* @create: 2019-07-24 22:25
**/
class Point<T>{
private T x;
private T y;
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
}
public class TestDemo {
public static void main(String[] args) {
Point<String> point=new Point<>();
point.setX("东经30度");
// point.setY(90);此行会报错//避免使用Object类型向下转型
point.setY("西经0度");
String x=point.getX();
String y=point.getY();
System.out.println("x="+x+",y="+y);
}
}