继承的java题,求源代码,急需,谢谢

2025-05-16 08:52:33
推荐回答(1个)
回答1:

package test;

public class TotalArea {

private Geometry tuxixing[] = new Geometry[29];

Double totalArea;

public void setTuxing(Geometry[] t) {
for (int i = 0; i < t.length; i++) {
if (i % 2 == 0) {
tuxixing[i] = new Rect(new Double(16 + i), new Double(60));
}
if (i % 2 == 1) {
tuxixing[i] = new Circle(new Double(10 + i));
}

}
}

public Double computerToTatalArea() {
Double totalArea = new Double(0);
for (int i = 0; i < tuxixing.length; i++) {
totalArea += tuxixing[i].getArea();
}

return totalArea;
}
}


package test;

public class Circle implements Geometry{

private Double r;

public Circle(Double r) {
this.r=r;
}
public Double getArea() {
return r;
}

}
package test;

public class Rect implements Geometry {
private Double a;
private Double b;

public Rect(Double a, Double b) {
this.a = a;
this.b = b;
}

public Double getArea() {
return a + b;
}

}
package test;

public interface Geometry {


Double getArea();
}