我需要帮助创建一个具有私有实例变量的 Rectangle 类,但公共方法和构造函数

I need help creating a Rectangle class with private instance variables, but public methods and constructors

提问人:Sheebs 提问时间:11/2/2023 更新时间:11/6/2023 访问量:77

问:

我是使用 java 的新手,我需要创建一个名为 Rectangle 的类,但我使用的代码似乎不起作用,并且类的注释没有帮助。我觉得这与我的方法有关,因为我不太了解这些方法。

该类需要遵循以下要求: 使用以下实例变量和公共构造函数创建一个名为 rectangle 的类:

  • 宽度,双倍

  • 双倍高度

  • 具有一个名为 sideLength 的参数 (double) 的构造函数,该参数将宽度和高度都设置为 sideLength。

  • 具有两个参数 width 和 height (double) 的构造函数,它将 width 和 height 设置为传递的相应值。

  • 一个没有参数的公共方法 computeArea,它以 double 形式返回 Rectangle 的面积

  • 一个不带参数的公共方法 computePerimeter,它以 double 形式返回 Rectangle 的周长

  • 一个没有参数的公共方法 getHeight,它以 double 形式返回 Rectangle 的高度

  • 一个没有参数的公共方法 getWidth,它以 double 形式返回 Rectangle 的宽度

我不需要制作一个主要方法,因为已经提供了。

这是我正在使用的代码,但我不断收到一个错误,说无法从computeArea方法中找到高度并向下。

 class Rectangle{
     
     private static double width;
     private static double heigth;
     
     public Rectangle(double sideLength) {
         
         Rectangle width = new Rectangle(sideLength);
         Rectangle height = new Rectangle(sideLength);
     }
         
     public Rectangle(double width, double height){
         width = width;
         height = height;
     }    

     public double computeArea() {
         return height * width;
     }
         
     public double computePerimeter() {
         return 2 * (height + width);
     }
         
     public double getHeight() {
         return height;
     }
     
     public double getWidth() {
         return width;
     }
     
 }
Java 方法 构造函数 instance-variables

评论

0赞 Rifat Rubayatul Islam 11/2/2023
你的代码应该抛出 .为什么在同一个构造函数内调用? 并且不应该是静态的。此外,在第二个构造函数中,它应该是 和 。StackOverflowErrornew Rectangleheightwidththis.width = widththis.height = height
0赞 Sheebs 11/2/2023
就像我说的,我是 java 的新手,所以我不完全确定我在做什么。我试着按照教授给我们的笔记去做,但他们没有太大帮助。我认为根据给出的注释调用新的矩形将是要使用的东西。
1赞 codebrane 11/2/2023
computeArea 具有“height”,但类具有“heigth”。这就是它无法编译的原因。
0赞 Sheebs 11/2/2023
@codebrane 你能解释一下你所说的不编译是什么意思吗?
1赞 Holger 11/2/2023
“我不完全确定我在做什么”不是借口。如果您不知道这意味着什么,请不要将其添加到代码中。即使对 Java 一无所知,只要大声朗读构造函数中的行:“width = new Rectangle”,你就会意识到这是多么奇怪。static

答:

1赞 codebrane 11/2/2023 #1

不要将私有变量设为静态变量,否则它们将由 Rectangle 的所有实例共享。

class Rectangle{
     
     private double width;
     private double height;
     
     public Rectangle(double sideLength) {
         
         this.width = sideLength;
         this.height = sideLength;
     }
         
     public Rectangle(double width, double height){
         this.width = width;
         this.height = height;
     }    

     public double computeArea() {
         return height * width;
     }
         
     public double computePerimeter() {
         return 2 * (height + width);
     }
         
     public double getHeight() {
         return height;
     }
     
     public double getWidth() {
         return width;
     }
 }

要在其他代码中使用它,请执行以下操作:

public class Test {
  public static void main(String[] args) {
    Rectangle square = new Rectangle(1.0);
    double area = square.computeArea();
    System.out.println(area);
    Rectangle rectangle = new Rectangle(1.0, 2.0);
    area = rectangle.computeArea();
    System.out.println(area);
  }
}

评论

0赞 Sheebs 11/2/2023
我尝试了您输入的代码,但我仍然像以前一样收到错误“找不到符号:高度”。
0赞 codebrane 11/2/2023
我添加了测试代码。我已经验证了它有效。您需要将 Rectangle 代码放在 Rectangle.java 文件中,将测试代码放在 Test.java 文件中。
0赞 Sheebs 11/9/2023
我已经意识到,当我使用您的代码时,我犯了拼写错误,但是一旦我修复了它,这确实有效。
1赞 Reilas 11/6/2023 #2

"...我不断收到一个错误,说无法从computeArea方法中找到高度并向下。..."

这是因为高度被声明为静态,因此无法从静态实例方法访问。

class Rectangle {
    double width, height;

    Rectangle(double sideLength) {
        width = height = sideLength;
    }

    Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public double computeArea() {
        return width * height;
    }

    public double computePerimeter() {
        return 2 * (width + height);
    }

    public double getHeight() {
        return height;
    }

    public double getWidth() {
        return width;
    }
}

或者,使用记录

record Rectangle(double width, double height) {
    Rectangle(double sideLength) {
        this(sideLength, sideLength);
    }
    
    public double computeArea() {
        return width * height;
    }
    
    public double computePerimeter() {
        return 2 * (width + height);
    }
}