从另一个类(不是主类)调用方法

时间:2013-07-13 08:39:29

标签: java class object distance

如何在一个方法下调用Point.java的distanceTo(Point p)到Point2.java不带参数?应该有办法,但我找不到我的材料。有人能帮助我吗?它已经做了2天。请帮忙......

--------------------- Point.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)));
    }
}

--------------------- ClonePoint.java -------------------- -------------

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)
    }

}

---------------------- PointDriver.java ------------------- ----------

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
    }
}

2 个答案:

答案 0 :(得分:1)

@Evan一个类是你的东西的通用容器。一辆车,一个人,一个点(在你的情况下)。

每当你想“创建”你定义的类的一个或多个对象时,你实例化它们:

Person evan = new Person();
Person rob = new Person();

我们都是人,你真的不需要定义课程Person1Person2

在类中,您应该定义用于“关联”到其他类似对象的方法。 例如:

// 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类。最后,只需在主体中初始化更多Point个对象(同一个类!)并使用它们。

希望有所帮助:)

修改

好吧,也许我已经完成了你的作业要求你做的事情。

“无参数”方法measureDistance()应该让你想知道一个重要的事情:“距离哪个点????”。

显然,如果函数没有参数,那么微积分所需的所有信息都必须在调用它的对象中。你不觉得吗?

所以,你可能想要实现一个辅助类(如果你真的需要将它定义为Point2它没关系,但改变那个名称,因为它很混乱),它可以在其构造函数中使用Point (将此信息保存在自身中),然后使用该点测量距离。

示例

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);
    }
    */

}

答案 1 :(得分:0)

首先,复制一个做同样事情的类并不是一个好主意,因为你正在做额外不需要的工作。其次,如果你制作了各种点类型,你就会失去它们之间无缝兼容的优势。

然后,如果你想从其他类调用方法,你可以这样做:

NameOfOtherClass.SomeMethod()

但是你必须将另一个类中的SomeMethod声明为静态...

public static double SomeMethod() { ... };

但是,您无法使用该方法访问您在代码中创建的具体点的数据,因此任何数据都应放入参数中。

如果你想按自己的方式去做,你只需要在public double measureDistance()添加一个参数 函数使函数可以访问另一个点来测量距离。