将方法作为参数传递

huangapple go评论81阅读模式
英文:

Pass method as parameter

问题

import java.util.Scanner;
import java.awt.geom.Point2D;

public class MyPoint {
    private double x1, y1;

    public MyPoint() {
        x1 = 0;
        y1 = 0;
    }

    public MyPoint(double x1, double y1) {
        this.x1 = x1;
        this.y1 = y1;
    }

    public double distance(MyPoint myPoint) {
        double distance = Math.sqrt(((myPoint.x1 - x1) * (myPoint.x1 - x1)) + ((myPoint.y1 - y1) * (myPoint.y1 - y1)));
        return distance;
    }

    public double distance(double x2, double y2) {
        double distance = Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
        return distance;
    }
}
英文:

I'm trying to create a method in which you calculate the distance between two MyPoint objects. I don't know how to pass a MyPoint object as an argument in the first distance method. Thanks

import java.util.Scanner;
import java.awt.geom.Point2D;

public class MyPoint {
 private double x1,y1;
 
    public MyPoint() {
        x1=0;
        y1=0;
        
    }
    public MyPoint(double x1, double y1) {
     this.x1 = x1;
     this.y1 = y1;
    }
    
    
    public double distance(myPoint) {
        
        
    }

    public double distance(double x2, double y2) {
        double distance = Math.sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
        return distance;
    }
}

答案1

得分: 2

你可以按照以下代码进行操作:

import java.util.Scanner;
import java.awt.geom.Point2D;

public class MyPoint {
    private double x, y;

    public MyPoint() {
        x = 0;
        y = 0;
    }

    public MyPoint(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double distance(MyPoint other) {
        double distance = Math.sqrt(((this.x - other.x) * (this.x - other.x)) + ((this.y - other.y) * (this.y - other.y)));
        return distance;
    }

    public static void main(String[] args) {
        MyPoint pointOne = new MyPoint(2.2, 3.3);
        MyPoint pointTwo = new MyPoint(3.3, 2.2);
        System.out.println(pointOne.distance(pointTwo));
    }
}
英文:

you can do as the following code

import java.util.Scanner;
import java.awt.geom.Point2D;

public class MyPoint {
 private double x,y;
 
    public MyPoint() {
        x=0;
        y=0;
        
    }
    public MyPoint(double x, double y) {
     this.x = x;
     this.y = y;
    }
    
    public double distance(MyPoint other) {
        double distance = Math.sqrt(((this.x-other.x)*(the.x-other.x))+((this.y-other.y)*(this.y-other.y)));
        return distance;
    }

public static void main(String[] args){
    pointOne = new Point(2.2,3.3);
    pointTwo = new Point(3.3,2.2);
    System.out.println(pointOne.distance(pointTwo));
}
}

答案2

得分: 1

你可以按照以下方式进行操作:

public class MyPoint {
 private double x1, y1;
 
  public MyPoint() {
      x1 = 0;
      y1 = 0;
  }

  public MyPoint(double x1, double y1) {
   this.x1 = x1;
   this.y1 = y1;
  }

  public double distance(final double x2, final double y2) {
    final double distance = Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
    return distance;
  }

  public double distance(final MyPoint myPoint) {
    return distance(myPoint.x1, myPoint.y1);
  }
}

public class Test {

  public static void main(final String[] args) {

    final MyPoint myPoint1 = new MyPoint(1, 1);
    final MyPoint myPoint2 = new MyPoint(2, 2);
    System.out.println(myPoint1.distance(myPoint2));

  }
}
英文:

You can do it as below :

public class MyPoint {
 private double x1,y1;
 
  public MyPoint() {
      x1=0;
      y1=0;
        
  }

  public MyPoint(double x1, double y1) {
   this.x1 = x1;
   this.y1 = y1;
  }
    

  public double distance(final double x2, final double y2) {
    final double distance = Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
    return distance;
  }

  public double distance(final MyPoint myPoint) {
    return distance(myPoint.x1, myPoint.y1);
  }

}

And you can call is as below:

public class Test {

  public static void main(final String[] args) {

    final MyPoint myPoint1 = new MyPoint(1, 1);
    final MyPoint myPoint2 = new MyPoint(2, 2);
    System.out.println(myPoint1.distance(myPoint));

  }
}

答案3

得分: 0

public double distance(MyPoint secondPoint) {
    //在这里计算它们之间的距离。
}

在你的主类中

MyPoint p1 = new MyPoint(10, 20);
MyPoint p2 = new MyPoint(20, 40);

System.out.println(p1.distance(p2));
英文:
public double distance(MyPoint secondPoint) {
    //Here you calculate the distance between them.
}

In your main class:

MyPoint p1=new MyPoint(10,20);
MyPoint p2=new MyPoint(20,40);

System.out.println(p1.distance(p2));

huangapple
  • 本文由 发表于 2020年10月19日 22:58:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64429967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定