0
我在代碼中遇到問題,我想打印對象的內容,例如在類(rect)中我想打印(x和y, ID .....) 知我它保存在一個鏈表作爲一個超類類型的對象, 看看我的課:如何打印對象的內容,而不是它的地址
public class rec extends Shape {
int x , id ;
double y ;
String style ;
int width , height ;
public rec ( int xs , double ys , int id , String st) {
x = xs ;
y = ys ;
style = st ;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
}
,中國保監會類:
public class circ extends Shape {
int cx , cy , r , id ;
String Style ;
public circ (int i , int cx1 , int cy1 , int re , String st) {
id = i;
cx = cx1;
cy = cy1 ;
r = re ;
Style = st ;
}
public int getCx() {
return cx;
}
public void setCx(int cx) {
this.cx = cx;
}
public int getCy() {
return cy;
}
public void setCy(int cy) {
this.cy = cy;
}
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
public String getStyle() {
return Style;
}
public void setStyle(String style) {
Style = style;
}
}
最後,超級班:
public class Shape {
final int width = 800;
final int higth = 600 ;
public int getWidth() {
return width;
}
public int getHigth() {
return higth;
}
}
主要:
rec re = new rec(9 , 94.4 , 5 , "F");
circ c = new circ(4 , 5 , 3 , 9 , "E");
LinkedList<Shape> r = new LinkedList<Shape>();
r.insert(re);
r.insert(c);
r.print(r);
打印方法是:
public static<T> void print(LinkedList<T> l) {
if (!l.empty()) {
l.findFirst();
while (!l.last()) {
System.out.println(l.retrieve());
l.findNext();
}
System.out.println(l.retrieve());
}
}
我有這樣的輸出:
[email protected]
[email protected]
任何幫助嗎?