我已分配使用抽象类作为超类“ Shape”的任务。它具有扩展该类的三个类,即Triangle,Circle和Square。每个类都应计算这些形状的面积和周长。当我在for循环中System.out.println(shapeArray[i].computeArea());
时,我的工作正常。但是,当我尝试在该for循环的子类中调用toString方法时,所有内容均返回为“ 0”。感谢您抽出宝贵的时间来帮助我,因为我很困在这里。我觉得我整天都在阅读,重新阅读主题和教科书。我觉得这里只有一个愚蠢的错误,我看不到。
Shape.java
public class Circle extends Shape {
private double radius;
final private double PI = 3.14159;
private double circumfrence;
private double area;
Circle(double radius) {
this.radius = radius;
}
@Override
public double computeArea() {
double radSquared = radius * radius;
double area = PI * radSquared;
return area;
}
@Override
public double computePerimeter() {
circumfrence = 2 * PI * radius;
return circumfrence;
}
@Override
public String toString() {
return "Area of Circle: " +area + "\nCircumfrence of Circle: " + circumfrence;
}
}
Triangle.java
public class Triangle extends Shape {
private double side1;
private double side2;
private double side3;
private double area;
private double perimeter;
Triangle (double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
public double computeArea() {
double s =(side1 + side2 + side3) / 2;
double area= Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
area = this.area;
return area;
}
@Override
public double computePerimeter() {
perimeter = side1 + side2 + side3;
return perimeter;
}
@Override
public String toString() {
return "Area of Triangle: " + area+ "\nPerimeter of Triangle: " + perimeter;
}
}
Circle.java
public class Circle extends Shape {
private double radius;
final private double PI = 3.14159;
private double circumfrence;
private double area;
Circle(double radius) {
this.radius = radius;
}
@Override
public double computeArea() {
double radSquared = radius * radius;
double area = PI * radSquared;
return area;
}
@Override
public double computePerimeter() {
circumfrence = 2 * PI * radius;
return circumfrence;
}
@Override
public String toString() {
return "Area of Circle: " +area + "\nCircumfrence of Circle: " + circumfrence;
}
}
Rectangle.java
public class Rectangle extends Shape {
private double length;
private double height;
private double sumOfLength;
private double sumOfHeight;
private double area;
private double perimeter;
Rectangle(double length, double height) {
this.height = height;
this.length = length;
}
@Override
public double computeArea() {
area = (length * height);
return area;
}
@Override
public double computePerimeter() {
sumOfLength = length * 2;
sumOfHeight = height * 2;
perimeter = sumOfLength + sumOfHeight;
return perimeter;
}
@Override
public String toString() {
return "Area of Rectangle: " +area + "\nPerimeter of Rectangle: " + perimeter;
}
}
ShapeArray.java
public class ShapeArray {
public static void main(String [] args) {
Shape shapeArray[] = new Shape[3];
Circle circ = new Circle(9);
Rectangle rect = new Rectangle(10, 6);
Triangle tri = new Triangle(6, 8, 12);
shapeArray[0] = circ;
shapeArray[1] = rect;
shapeArray[2] = tri;
for (int i = 0; i <= 2; i++) {
System.out.println(shapeArray[i].toString());
}
}
}
输出:
Area of Circle: 0.0
Circumfrence of Circle: 0.0
Area of Rectangle: 0.0
Perimeter of Rectangle: 0.0
Area of Triangle: 0.0
Perimeter of Triangle: 0.0
您的Triangle.computeArea
类方法将从area = this.area;
更改为this.area=area;
其他类似的课程。
在shapeArray[i].computeArea()
之前,您必须先调用for循环shapeArray[i].computePerimeter()
和shapeArray[i].toString()
。
在ShapeArray.java中>
//other code for (int i = 0; i <= 2; i++) { shapeArray[i].computeArea(); shapeArray[i].computePerimeter() System.out.println(shapeArray[i].toString()); } //other code
它打印0,因为在打印之前您不调用类实例的周长和面积的计算。但我也建议您检查您的代码,不要使用不必要的变量。]>