繁体   English   中英

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

[英]Method from other class and use of toString()

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

收到关于 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;
    }

}

如果你想通过你的类Triangle toString()方法接收距离计算,你只需要像方法calculate一样实现它:

使用toString可能解决方案

你的类看起来像这样:

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方法不依赖于在类中声明的任何变量 ( x,y,x1,y1 ),而只依赖于参数。 所以它是独立的,可以设为static 因此,这将使它成为一种可以从外部调用的实用方法,例如Pytaguras.calculate(...) 请参阅关于static 的Java 教程

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

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

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());

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM