JAVA、setter 没有将值发送到我的构造函数返回零

JAVA, setter is not geting the values sent to my constructor program returning zeros

我遇到的错误是控制台打印出点值中的所有零,而它应该显示类似于 (1,2) (3,4) (5,6) (7,8)

我追踪到 setter 的错误,它没有加载值。 示例

 public void setPoint1(double newX, double newY) {
  this.N1x = newX;
  this.N1y = newY;

}

这不会将 private double N1.x 更新为相应的构造函数值 1。我认为这是问题所在,但我是编码新手,还有很多东西要学。

据我了解,在构造函数中调用 setter 应该从构造函数调用中获取 1 到 8 的值,然后发送到 setter 然后分发它们到私有双打,然后执行 Point Class 操作,并将值保存到 point1 到 point4 对象中,然后从 main 方法调用返回它们存储的值。

下面是关于我的问题的代码。先感谢您!

public class Quadrilateral {

   //Receives data from constructor 
   //initializes the variables for point objects  
   private double N1x; private double N1y;
   private double N2x; private double N2y;
   private double N3x; private double N3y;
   private double N4x; private double N4y;

   //creates new point objects
   Point point1 = new Point(N1x,N1y);
   Point point2 = new Point(N2x,N2y);
   Point point3 = new Point(N3x,N3y);
   Point point4 = new Point(N4x,N4y);

   // Encapsulation for point 1
   public Point getPoint1() {
      return point1;
   }
   public void setPoint1(double newX, double newY) {
      this.N1x = newX;
      this.N1y = newY;
   }

   // Encapsulation for point 2
   public Point getPoint2() {
      return point2;
   }
   public void setPoint2(double newX, double newY) {
      this.N2x = newX;
      this.N2y = newY;
   }

   // Encapsulation for point 3
   public Point getPoint3() {
      return point3;
   }
   public void setPoint3(double newX, double newY) {
      this.N3x = newX;
      this.N3y = newY;
   }

   // Encapsulation for point 4
   public Point getPoint4() {
      return point4;
   }
   public void setPoint4(double newX, double newY) {
      this.N4x = newX;
      this.N4y = newY;
   }

   //constructor for Quadrilateral
   //takes in 4 sets of point values
   Quadrilateral(
         double N1x, double N1y,
         double N2x, double N2y,
         double N3x, double N3y,
         double N4x, double N4y){

      setPoint1(N1x, N1y);
      setPoint2(N2x, N2y);
      setPoint3(N3x, N3y);
      setPoint4(N4x, N4y);

   }

   // gets the (x,y) values 
   //remember to override to add additional values and change shape

   public String toString() {
      return "Quadrilateral "+"\n"+ 
            "Node points are "+"\n"+
            getPoint1()+"\n"+
            getPoint2()+"\n"+
            getPoint3()+"\n"+
            getPoint4();
   }

public static void main(String[] args) {

   Quadrilateral quadrilateral = new Quadrilateral(1,2,3,4,5,6,7,8);
   System.out.println(quadrilateral.toString());
   }

}

这是重点class

public class Point {
   private double x;
   private double y;

   public double getX() {
      return x;
   }
   public double getY() {
      return y;
   }
   public void setX(double newX) {
      this.x = newX;
   }
   public void setY(double newY) {
      this.y= newY;
   }
   Point(double x, double y){//when this constructor is called it performs encapsulation 
      setX(x);
      setY(y);
   }
   public String toString() {
      return "("+getX()+","+getY()+")";
   }
}

如果我理解你的问题,你应该这样做:

//creates new point objects
Point point1;
Point point2;
Point point3;
Point point4;

还有这个:

public void setPoint1(double newX, double newY) {
    this.N1x = newX;
    this.N1y = newY;
    point1 = new Point(newX, newY);
}

(其他积分设定者也一样)

在您的代码中,您仅在坐标为空时初始化了点,甚至在您首次在 setPoint 方法中设置坐标之前

由于您正在初始化您的 Point 对象,Quadrilateral 的成员变量甚至在使用 N1x、N2x 等未初始化的构造函数调用之前,您正在设置它们的默认值,即 Point 对象(成员变量)中的 0 .更改您的四边形 class 定义如下

public class Quadrilateral {

    // creates new point objects
    Point point1 ;
    Point point2;
    Point point3 ;
    Point point4 ;

    // Encapsulation for point 1
    public Point getPoint1() {
        return point1;
    }

    public void setPoint1(double newX, double newY) {
        this.point1 = new Point(newX, newY);        
    }

    // Encapsulation for point 2
    public Point getPoint2() {
        return point2;
    }

    public void setPoint2(double newX, double newY) {
        this.point2 = new Point(newX, newY);
    }

    // Encapsulation for point 3
    public Point getPoint3() {
        return point3;
    }

    public void setPoint3(double newX, double newY) {
        this.point3 = new Point(newX, newY);
    }

    // Encapsulation for point 4
    public Point getPoint4() {
        return point4;
    }

    public void setPoint4(double newX, double newY) {
        this.point4 = new Point(newX, newY);
    }

    // constructor for Quadrilateral
    // takes in 4 sets of point values
    Quadrilateral(double N1x, double N1y, double N2x, double N2y, double N3x, double N3y, double N4x, double N4y) {

        setPoint1(N1x, N1y);
        setPoint2(N2x, N2y);
        setPoint3(N3x, N3y);
        setPoint4(N4x, N4y);

    }

    // gets the (x,y) values
    // remember to override to add additional values and change shape

    public String toString() {
        return "Quadrilateral " + "\n" + "Node points are " + "\n" + getPoint1() + "\n" + getPoint2() + "\n"
                + getPoint3() + "\n" + getPoint4();
    }

    public static void main(String[] args) {

        Quadrilateral quadrilateral = new Quadrilateral(1, 2, 3, 4, 5, 6, 7, 8);
        System.out.println(quadrilateral.toString());
    }

}