class Point<T>
{
private T x;
private T y;
public T getX()
{
return x;
}
public T getY()
{
return y;
}
public void setPoint(T x,T y)
{
this.x=x;
this.y=y;
}
}
public class test
{
public static void main(String args[])
{
Point<Integer> p1=new Point<Integer>();
p1.setPoint(2,3);
System.out.println("p1's locate is ("+p1.getX()+","+p1.getY()+")");
Point<Double> p2=new Point<Double>();
p2.setPoint(2.2,3.3);
System.out.println("p2's locate is ("+p2.getX()+","+p2.getY()+")");
Point<String> p3=new Point<String>();
p3.setPoint("120E","120W");
System.out.println("p3's locate is ("+p3.getX()+","+p3.getY()+")");
}
}
实例化的时候指定类型
范型
最新推荐文章于 2019-12-03 20:14:38 发布