package test; /** * 实现泛型的概念 * @author Administrator * */ public class Point<T> { //T可以使用任意标记,例如A,B 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; } } package test; /** * 正确使用泛型 * @author Administrator * */ public class TestPoint { public static void main(String[] args) { Point<String> point = new Point<String>(); //实例化对象,设置了泛型的类型 point.setX("东经100°"); point.setY("北纬20°"); System.out.println("x的坐标为:"+ point.getX()+"y的坐标为:"+ point.getY()); Point point2=new Point();//实例化对象,没有设置泛型的类型,设置x或y的内容的时候可以设置为任意的类型 point2.setX(10); point2.setY("南纬50°"); System.out.println("\nx的坐标为:"+point2.getX()+ "y的坐标为:"+ point2.getY()); } } 这是个人理解,如有错误,请指正