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

2024-10-28 19:20:30
推荐回答(2个)
回答1:

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);

}

}

回答2:

泛型,通俗点就是提前声明这个list中将来到底装什么东西
lines的泛型为list
list的泛型又是Point对象
通俗说法:lines中存放一堆list列表
而每个list中又存放一堆Point对象
跟多维数组很类似:
lines1
---list1
----Point1
----Point2
----Point3
----Point4
---list2
----Point5
----Point6
----Point7
----Point8
lines2
---list2
----Point1
----Point2
----Point3
----Point4
---list3
----Point5
----Point6
----Point7
----Point8