[英]Java: How to use the toString() method of one class in an other one?
[英]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.