ArrayList集合存储自定义对象并操作对象
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