【问题标题】:I'm not understanding what the error comment is trying to tell me我不明白错误评论试图告诉我什么
【发布时间】:2014-08-05 10:01:45
【问题描述】:

我有一个名为 Circle 的类和另一个名为 POINT 的类,因此 Circle 由半径和点(即圆的中心点)组成。我正在尝试构建一个构造函数,但遇到了一条错误消息和我无法弄清楚这个问题。

(类圈,它的开头)

public class Circle extends Shape {
    private int radius, x, y;

    Point point = new Point(0,0);

    public Circle(radius,x, y){
        this(5, 3, 6);
    }

课点:

    public class Point {
    private int xPos,yPos;
    public Point(int x, int y){
        setxPos(x);
        setyPos(y);
    }
    public int getxPos() {
        return xPos;
    }
    public void setxPos(int xPos) {
        this.xPos = xPos;
    }
    public int getyPos() {
        return yPos;
    }
    public void setyPos(int yPos) {
        this.yPos = yPos;
    }
    ...

【问题讨论】:

  • 究竟是什么错误信息?
  • 而错误信息是...?
  • 是的,很有意思:这条信息真的很清楚。
  • -令牌“,”的语法错误,删除此令牌
  • 基本修复:public Circle(int radius, int x, int y)

标签: java class object extends


【解决方案1】:

改变

     public Circle(radius,x, y){
       this(5, 3, 6);
   }

  public Circle(int radius,int x, int y){  //need to add types to parameters
              this.radius=radius;
              this.x=x;
              this.y=y;
   }

或者如果您打算使用 5,3,6 作为常量值添加

  public Circle(){ 
              this.radius=5;
              this.x=3;
              this.y=6;
   }

【讨论】:

  • 现在它说递归构造函数调用 (int,int,int)
【解决方案2】:

您的构造函数是 recursive 尽管您的声明是错误的,正如其他答案所建议的那样,我建议您更改声明并重新考虑您可能想要在构造函数中初始化 radius,x and y 的代码,并且您可能想要声明一些方法对初始化值进行操作。

 public Circle(int radius,int x, int y){
  //Remove this from the constructor and perform initialization
  this.radius=radius;
  this.x=x;
  this.y=y
 }

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 2022-11-30
    • 2019-02-20
    相关资源
    最近更新 更多