Setter 和 Getter 以及子类的问题

Troubles with Setters and Getters and Subclasses

几天来我一直在做一项作业,一直在用头撞墙。基本上假设为类似形状的多边形设置一个超类,然后封装一些数据并使用 setter 和 getter 将该信息调用到子类中。我一直在一遍又一遍地重新阅读我们的书,并在线观看了大量教程,但它就是不适合我。这是我目前为超类所做的一个示例:

public class Polygon {
   private double Sides;
   private double Length;
   private double Width;
   private double Height;

public double calcArea();
public double calcPerimeter();
}

想法是子类将能够定义参数。 我认为这是正确的,但是当我开始我的子类时我失去了它,这是我现在第一个子类的内容,根据建议编辑:

  public class Triangle extends Polygon {

    public Triangle(){
    }

     public void Triangle (double Base, double Height) {
                this.Width = Base;
                this.Height = Height;
                this.Length = Length;
    }

    public double getWidth;
        return this.Width;
}
    public static setWidth(double Width){
        this.Width = 10;

}
    public double getHeight;
        return this.Height;

}
    public static setHeight(double Height){
        this.Height = 10;
    }
    public double getLength;
        return this.Length;
}
    public static setLength(double Width){
        this.Height = 10;
    }

    @Override
    public calcArea() {
        return 0.5 * Width * Height;
        }
    @Override
    public double calcPerimeter() {
        return Length + Length + Length;
    }
}

这是一项正在进行的巨大工作,所以我知道它有点乱,但我几乎每一行都抛出了错误,我不知道我在做什么 set/get错误的。任何指向我正确方向的帮助将不胜感激,谢谢。

现在我得到的错误是: 第 7/8/9 行:宽度。高度、长度在多边形中具有私有访问权限

第 13 行:表达式的非法开头

然后是第 15/18/19/20/22/23/25/26/27/28/29/31/34/38/40 行:Class、接口或枚举。

编辑:有人建议我删除摘要,所以我就这么做了。

首先你应该注意这不是你在 Java 中声明函数的方式。

  public double getLength;
        return this.Length;
}

但这应该是

 public double getLength(){
        return this.Length;
}

第二件事,如果你声明所有 classes 抽象那么你不能创建对象(通过使用 new 关键字)。 顺便说一句,这是一个工作版本:

 public class Triangle extends Polygon {

    public Triangle(double base,double height,double length){
      super(base,height,length);  
     }


    @Override
    public double calcArea() {
        return 0.5 * this.getWidth() * this.getHeight();
        }
    @Override
    public double calcPerimeter() {
        return this.getLength() + this.getLength() + this.getLength();
    }


    //tests

    public static void main(String [] args) {
        Triangle triangle = new Triangle(10,20,30);
        System.out.println(triangle.getWidth() + " width of a triangle");
        System.out.println(triangle.getHeight() + " height of a triangle");
        System.out.println(triangle.getLength() + " length of a triangle");

    }
}

如果你想给 super class(sides) 也提供第 4 个参数,那么你可以创建一个带有 4 个参数的额外构造函数而不删除这个。

这是你的多边形 class:

public abstract class Polygon {
   private double Length;
   private double Width;
   private double Height;

public Polygon(double base, double height, double length) {
           this.Width = base;
           this.Height = height;
           this.Length = length;
}

public abstract double calcArea();
public abstract double calcPerimeter();


public double getWidth() {
    return this.Width;
}

public void setWidth(double Width){
    this.Width = Width;

}

public double getHeight() {
    return this.Height;

}
public void setHeight(double Height){
    this.Height = Height;
}
public double getLength() {
    return this.Length;
}

public void setLength(double length){
    this.Length = length;
 }
}