繁体   English   中英

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

[英]Call a method from another class(not main class)

如何在不带参数的情况下将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
    }
}

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

}

首先,复制一个做同样事情的类不是一个好主意,因为您正在做多余的工作。 其次,如果您创建各种点类型,那么您将失去它们之间无缝兼容的优势。

然后,如果要从其他类调用方法,可以这样进行:

NameOfOtherClass.SomeMethod()

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

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

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

如果要按照自己的方式进行操作,则只需在public double measureDistance()函数中添加一个参数,以便该函数可以访问另一个要测量距离的点。

暂无
暂无

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

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