来自其他 class 的方法和 toString() 的使用

Method from other class and use of toString()

我正在尝试使此代码正常工作,从 MAIN 上的用户接收 2 点 (x, y) & (x1,y1) 并使用另一个 class 的方法并进行计算。

此外,如果能收到有关 toString 的解释,那就太好了。

import java.util.Scanner;
public class Test{
    public static void main(String[]args)
    {
        Scanner scan = new Scanner(System.in);
        double x , y , x1 , y1;
        Pytaguras Triangle = new Pytaguras();

        System.out.println("Hello , this program is using Pytagoras formula" +
        " on the 4 point that you enter");
        System.out.println("Please enter 4 points to calculate the distance");
        x = scan.nextDouble();
        y = scan.nextDouble();
        x1 = scan.nextDouble();
        y1 = scan.nextDouble();
        Triangle.setDouble( x, y,  x1, y1);
        System.out.println("the distance between 2 points is :" + Triangle.calculate() );

    }
}

public class Pytaguras
{
    private double x, y, x1 ,y1 ;
    public void setDouble(double _x, double _y, double _x1, double _y1)
    {   _x=x;
        _y=y;
        _x1=x1;
        _y1=y1;}

    public double calculate(double _x , double _y , double _x1 , double _y1 )
    {   double s;
         s = Math.sqrt((_x-_y)*(_x-_y)+(_x1-_y1)*(_x1-_y1));
        return s;
    }

}

您可以通过 class 构造函数简单地分配值。

public class Triangle {
    private double x, y, x1 , y1;

    public Triangle(double new_x, double new_y, double new_x1, double new_y1) {
        x = new_x;
        y = new_y;
        x1 = new_x1;
        y1 = new_y1;
    }

    public double calculate() {   
        return Math.sqrt((x-y)*(x-y)+(x1-y1)*(x1-y1));
    }

}

然后在您的主菜单上:

public static void main(String[]args) {
        Scanner scan = new Scanner(System.in);
        double x , y , x1 , y1;
        Pytaguras Triangle = new Pytaguras();

        System.out.println("Hello , this program is using Pytagoras formula" +
        " on the 4 point that you enter");
        System.out.println("Please enter 4 points to calculate the distance");
        x = scan.nextDouble();
        y = scan.nextDouble();
        x1 = scan.nextDouble();
        y1 = scan.nextDouble();
        Triangle triangle = new Triangle(x, y, x1, y1);
        System.out.println("the distance between 2 points is :" + triangle.calculate());

}

如果您想通过 class Triangle 的方法 toString() 接收 距离计算,您只需要执行它与方法 calculate:

相同

可能的解决方案使用toString

您的 class 看起来像这样:

public class Pytaguras {
    private double x, y, x1 ,y1 ;
    public void setDouble(double _x, double _y, double _x1, double _y1) {
        _x=x;
        _y=y;
        _x1=x1;
        _y1=y1;
    }

    public static double calculate(double _x , double _y , double _x1 , double _y1 ) {
        return Math.sqrt((_x-_y)*(_x-_y)+(_x1-_y1)*(_x1-_y1));
    }

    public String toString() {
        return "Distance is " + Math.sqrt((x - y)*(x -y)+(x1 - y1)*(x1 - y1));
    }
}

可能出了什么问题?

当您尝试像这样实现 toString() 时:

// code omitted for clarity

public double calculate(double _x , double _y , double _x1 , double _y1 ) {   
  double s; // variable declared locally, thus can be used only inside this method
  s = Math.sqrt((_x-_y)*(_x-_y)+(_x1-_y1)*(_x1-_y1));
  return s;
}

// code omitted for clarity

public String toString() {
  return s; // will give compiler-error, since symbol/variable "s" is not visible here, but only inside method calculate
}

// code omitted for clarity

重构 = 优化代码

在实用程序方法上使用 static 您的 calculation 方法不依赖于在 class,但仅限于参数。所以它独立,可以static。因此可以使它成为一个 实用方法 ,可以像 Pytaguras.calculate(...) 一样从外部调用。参见 Java Tutorial on static

使用构造函数初始化变量:你的2点可以直接使用构造函数初始化。因此你不需要同时调用 new Pytaguras()setDouble(x1, y1, x2, y2)。您可以简单地调用 new Pytaguras(x1, y1, x2, y2)。参见 Java Tutorial on constructor