作业帮 > 综合 > 作业

JAVA编程问题 定义一个泛型类Point,它包含横坐标x和纵坐标y两个变量,类型均为T

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/23 02:03:24
JAVA编程问题 定义一个泛型类Point,它包含横坐标x和纵坐标y两个变量,类型均为T
该类具有两个参数的构造方法、x和y的设置器与访问器、输出方法.在main()函数中分别传入Double、Float、Integer类型数据加以验证.
public class Point {
private T x;
private T y;
public Point(T x, T y) {
this.x = x;
this.y = 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 void outPut(Point p){
System.out.println("点的x坐标为:"+p.getX());
System.out.println("点的y坐标为:"+p.getY());
}

public static void main(String[] args) {
Point p=new Point(1,2);
p.outPut(p);

Point d=new Point(1.0,2.0);
d.outPut(d);

Point f=new Point(1.0f,2.0f);
f.outPut(f);

}
}