ArrayList集合存储自定义对象并操作对象

qq_42172623 2018-05-09 07:30:19
package TURTLES;
import java.util.ArrayList;
import java.util.Iterator;

public class Arraylist{
public static void main(String []args) {
ArrayList <shape>Array = new ArrayList<shape>();
shape rec = new rectangle(0.9,0.1,90.0); // 类 rectangle Square circle 是 shape 的子类
shape squ = new Square(0.7,0.3,90.0);
shape cir = new circle(0.5,0.5,10.0);
Array.add(rec); // 添加对象
Array.add(squ);
Array.add(cir);
Iterator <shape>it = Array.iterator();
while(it.hasNext()) { // 循环遍历arrylist
shape T = (shape)it.next();
System.out.println(T.GetX()+" "+T.GetY()+" "+T.GetA());
// System.out.println(it.next()); // 输出类名 和hashcode

}
}
}
class shape extends Turtle{
double x;
double y;
double a;
public shape(double x0,double y0,double a0) {
super(x0,y0,a0);
}
public double GetX() {
return x;
}
public void SetX(double x0) {
this.x=x0;
}
public double GetY() {
return y;
}
public void SetY(double y0) {
this.y=y0;
}
public double GetA() {
return a;
}
public void SetA(double a0) {
this.a=a0;
}

}
我想输出对象属性X Y Z的值 可是运行结果都是 0 0 0
...全文
1851 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38519136 2018-05-10
  • 打赏
  • 举报
回复
你把成员变量的值付给了子类,你却要调用父类的方发,,你的写一个方法,使用多态,形式参数写父类,实际参数传子类对象,这样应该就可以了
  • 打赏
  • 举报
回复
把你的子类跟父类代码贴出来瞅瞅 目前来看,可能是你的子类有了新的构造器。

58,453

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧