JAVA编程问题 定义一个泛型类Point,它包含横坐标x和纵坐标y两个变量,类型均为T
该类具有两个参数的构造方法、x和y的设置器与访问器、输出方法。在main()函数中分别传入Double、Float、Integer类型数据加以验证。...
该类具有两个参数的构造方法、x和y的设置器与访问器、输出方法。在main()函数中分别传入Double、Float、Integer类型数据加以验证。
展开
2个回答
展开全部
public class Point<T> {
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<T> p){
System.out.println("点的x坐标为:"+p.getX());
System.out.println("点的y坐标为:"+p.getY());
}
public static void main(String[] args) {
Point<Integer> p=new Point<Integer>(1,2);
p.outPut(p);
Point<Double> d=new Point<Double>(1.0,2.0);
d.outPut(d);
Point<Float> f=new Point<Float>(1.0f,2.0f);
f.outPut(f);
}
}
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<T> p){
System.out.println("点的x坐标为:"+p.getX());
System.out.println("点的y坐标为:"+p.getY());
}
public static void main(String[] args) {
Point<Integer> p=new Point<Integer>(1,2);
p.outPut(p);
Point<Double> d=new Point<Double>(1.0,2.0);
d.outPut(d);
Point<Float> f=new Point<Float>(1.0f,2.0f);
f.outPut(f);
}
}
来自:求助得到的回答
展开全部
泛型,通俗点就是提前声明这个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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询