Java 从另一个类(非主类)调用方法
在不带参数的方法下,如何将Point.java的distanceTo(Point p)调用为Point2.java?应该有办法,但我无法从我的材料中找到。有人能帮我吗?已经做了两天了。请帮忙 --------------Point.java--------------------Java 从另一个类(非主类)调用方法,java,class,object,distance,Java,Class,Object,Distance,在不带参数的方法下,如何将Point.java的distanceTo(Point p)调用为Point2.java?应该有办法,但我无法从我的材料中找到。有人能帮我吗?已经做了两天了。请帮忙 --------------Point.java-------------------- public class Point{ private int x; private int y; //x and y coordinates as parameters public
public class Point{
private int x;
private int y;
//x and y coordinates as parameters
public Point(int x, int y){
this.x = x;
this.y = y;
}
//I want to call this method by calling a method which taken no parameter in Point2.java.
public double distanceTo(Point p){
return Math.sqrt(((x - p.x) * (x - p.x)) + ((y - p.y) * (y - p.y)));
}
}
public class ClonePoint{
private int a;
private int b;
//x and y coordinates as parameters
public ClonePoint(int a, int b){
this.a = a;
this.b = b;
}
//I failed with this way. Can anybody correct me?
public double measureDistance(){//it should be takes no parameter.
return distanceTo(ClonePoint p)
}
}
public class PointDriver {
public static void main(String [] args) {
Point2 nn = new Point2(11, 22);
Point2 mm = new Point2(33, 44);
System.out.println(nn.distanceTo(mm)); //I succeeded with this!
System.out.println(nn.measureDistance(mm)); //But I got an error illegal start of expression
}
}
--------------ClonePoint.java--------------------
public class Point{
private int x;
private int y;
//x and y coordinates as parameters
public Point(int x, int y){
this.x = x;
this.y = y;
}
//I want to call this method by calling a method which taken no parameter in Point2.java.
public double distanceTo(Point p){
return Math.sqrt(((x - p.x) * (x - p.x)) + ((y - p.y) * (y - p.y)));
}
}
public class ClonePoint{
private int a;
private int b;
//x and y coordinates as parameters
public ClonePoint(int a, int b){
this.a = a;
this.b = b;
}
//I failed with this way. Can anybody correct me?
public double measureDistance(){//it should be takes no parameter.
return distanceTo(ClonePoint p)
}
}
public class PointDriver {
public static void main(String [] args) {
Point2 nn = new Point2(11, 22);
Point2 mm = new Point2(33, 44);
System.out.println(nn.distanceTo(mm)); //I succeeded with this!
System.out.println(nn.measureDistance(mm)); //But I got an error illegal start of expression
}
}
--------------------------PointDriver.java----------------------
public class Point{
private int x;
private int y;
//x and y coordinates as parameters
public Point(int x, int y){
this.x = x;
this.y = y;
}
//I want to call this method by calling a method which taken no parameter in Point2.java.
public double distanceTo(Point p){
return Math.sqrt(((x - p.x) * (x - p.x)) + ((y - p.y) * (y - p.y)));
}
}
public class ClonePoint{
private int a;
private int b;
//x and y coordinates as parameters
public ClonePoint(int a, int b){
this.a = a;
this.b = b;
}
//I failed with this way. Can anybody correct me?
public double measureDistance(){//it should be takes no parameter.
return distanceTo(ClonePoint p)
}
}
public class PointDriver {
public static void main(String [] args) {
Point2 nn = new Point2(11, 22);
Point2 mm = new Point2(33, 44);
System.out.println(nn.distanceTo(mm)); //I succeeded with this!
System.out.println(nn.measureDistance(mm)); //But I got an error illegal start of expression
}
}
首先,复制一个做同样事情的类不是一个好主意,因为您正在做额外的不必要的工作。第二,如果您创建了各种点类型,您将失去它们之间无缝兼容性的优势 然后,如果您想从其他类调用方法,可以这样做:
NameOfOtherClass.SomeMethod()
但是您必须将另一个类中的SomeMethod声明为static
public static double SomeMethod() { ... };
但是,您不能使用该方法访问在代码中创建的具体点的数据,因此任何数据都应该放入参数中
如果您想按自己的方式操作,只需向public double measureDistance()
函数,这样函数就可以访问另一个点来测量距离。@Evan类是一个通用的容器,用于存放物品。一辆车,一个人,一个观点(在你的情况下) 每次要“创建”已定义类的一个或多个对象时,都要实例化它们:
Person evan = new Person();
Person rob = new Person();
我们都是人,你真的不需要定义类Person1
和Person2
在类中,您应该定义用于与其他类似对象“关联”的方法。
例如:
// In Person.java
public void greet(Person p) {
System.out.println("My name is "+this.name+". Nice to meet you +"p.getName());
}
// In main
rob.greet(evan); // it now gives compile errors of course but take the point :P
您想要实现的是创建一个更好、更完整的Point
类,其中包含您想要使用的所有方法。最后,只需在main中初始化更多的点
对象(同一类!),然后使用它们
希望有帮助:)
编辑
好吧,也许我已经知道你的家庭作业想要你做什么了
“无参数”方法measureDistance()
应该让您想知道一件重要的事情:“与哪个点的距离??”
显然,如果函数不带参数,那么演算所需的所有信息都必须在调用它的对象中。你不觉得吗
因此,您可能想要实现一个二级类(如果您真的需要将其定义为Point2
,这没关系,但更改名称是因为它容易混淆),该类可以在其构造函数中获取一个点(将此信息保存在自身中),然后使用该点测量与它的距离
示例
public class Point2{
private int a;
private int b;
private Point startingPoint;
public Point2(int a, int b, Point p){
this.a = a;
this.b = b;
startingPoint = p;
}
// Computes the distance from starting point to this
public double measureDistance(){//it takes no parameter.
return startingPoint.distanceTo(a, b);
}
/*
if you can't edit distanceTo() it gets a little verbose but you must create a
Point with Point2 coordinates - remember this example when you will study Inheritance
public double measureDistance() {
Point endingPoint = new Point(a, b);
return startingPoint.distanceTo(endingPoint);
}
*/
}
谢谢你的回复。我将在后面的章节中学习性能,但这是家庭作业,所以我必须做=),条件是public double measureDistance()不接受任何参数,而是从其他类调用distanceto(Point p)方法。感谢您的输入。它可以在构造器中取一个点(将此信息保存在自身中),然后使用该点测量与它的距离。。。。你能给我举个例子吗?我会在distanceTo(int x,int y)
中编辑第一个函数distanceTo(Point p)
,因为当你想从NewClass.measureDistance()使用它时,你不能使用点。我将用代码示例编辑我的答案谢谢Rob!我将等待您的示例=)非常感谢您的建议和代码Rob!您的详细评论对我帮助很大=)我现在将研究继承。我想有一天成为像你这样的java天才@埃文斯:如果只写几个代码片段就能得到“天才”的头衔祝你学习顺利!(记住你认为有用的投票答案,不管你是否接受)