使用抽象类时找不到符号

问题描述 投票:-2回答:1

练习具有形状的抽象类。目标是使用抽象类获得3个形状的总面积。到目前为止,这就是我所拥有的。

我不确定我是否正确执行了此部分:

    static double sumArea(Shape[] arr){
        // Sum up the areas of all the shapes using getArea()
        return arr.getArea();
    }

我不断收到错误消息,指出找不到符号h(高度),w(宽度),tw(top_width)。有人知道为什么找不到这些符号吗?


public class TestShape{
    public static void main(String args[]){
        Point p = new Point(1, 1);

        Shape[] arr = {
            new Rectangle(p, 3, 4),
            new Parallelogram(p, 5, 6, Math.PI/6.0),
            new Trapezoid(p, 5, 6, 2)
        };

        System.out.println("SUM_AREA = " + sumArea(arr));
    }

    static double sumArea(Shape[] arr){
        // Sum up the areas of all the shapes using getArea()
        return arr.getArea();
    }
}

class Point{
    double x, y;

    Point(){
        this(0, 0);
    }

    Point(double x, double y){
        this.x = x;
        this.y = y;
    }

    public String toString(){
        return "[" + x + ", " + y + "]";
    }
}

abstract class Shape{
    Shape(){

    }

    Shape(Point p){ 
        this.p = p;
    }

    public Point getPosition(){
        return p; 
    }

    public void setPosition(Point p){
        this.p = p;
    }

    // Abstract method
    public abstract double getArea(); 
}

abstract class Quadrangle extends Shape{
    protected double width, height;

    Quadrangle(Point p, double w, double h){
        this.p = p;
        this.width = w;
        this.height = h;
    }

    public double getWidth(){
        return w;
    }

    public double getHeight(){
        return h;
    }

    public void setWidth(double w){
        this.weight = w;
    }

    public void setHeight(double h){
        this.height = h;
    }
}

class Rectangle extends Quadrangle{
    Rectangle(Point p, double w, double h){
        this.p = p;
        this.width = w;
        this.height = h;
    }

    public boolean isSquare(){
        if(w == h){
            return "Error";
        }
    }

    @Override /** Return Area */
    public double getArea(){
        return w * h;
    }
}

class Parallelogram extends Quadrangle{
    protected double angle;

    Parallelogram(Point p, double w, double h, double angle){
        this.p = p;
        this.weight = w;
        this.height = h;
        this.angle = angle;
    }

    public double getAngle(){
        return angle;
    }

    public void setAngle(double a){
        this.angle = a;
    }

    @Override /** Return Area */
    public double getArea(){
        return w * h;
    }
}

class Trapezoid extends Quadrangle{
    protected double top_width;

    Trapezoid(Point p, double w, double h, double top_width){
        this.p = p;
        this.width = w;
        this.height = h;
        this.top_width = top_width;
    }

    public double getTopWidth(){
        return top_width;
    }

    public void setTopWidth(double tw){
        this.top_width = tw;
    }

    @Override /** Return Area */
    public double getArea(){
        return ((w + tw) / 2) * h;
    }
}
java overriding abstract-class
1个回答
0
投票

名称w,tw等仅作为参数存在。当您要访问保存在构造函数中的值时,必须使用左侧名称:this.[width or whatever]

此外,将sumArea重写为类似以下内容:

static double sumArea(Shape[] arr){
        // Sum up the areas of all the shapes using getArea()
        double totalArea = 0;
        for (Shape shape : arr) {
                totalArea += shape.getArea();
        }
        return totalArea;
}
© www.soinside.com 2019 - 2024. All rights reserved.