如何从另一个类调用非静态方法?

时间:2017-01-13 18:31:07

标签: java object methods

我有3个课程:PointPolygonClientePolygonPoint使用许多非静态方法创建一个对象Point(double x, double y)Polygon使用非静态方法创建一个Point数组,即写入,移动并返回最接近数组中原点的PointClientePolygon执行Polygon。问题是,当我运行ClientePolygon时,它给了我Point class executed...,我想运行Polygon。有什么建议?非常感谢。

ClientePolygon:

package k;
public class ClientePolygon {
    public void main(String [] args){
        Point origem = new Point (0,0);
        Point [] vertices = new Point[4];
        vertices[0] = new Point (1.0,1.0);
        vertices[1] = new Point (1.0,5.0);
        vertices[2] = new Point (2.0,5.0);
        vertices[3] = new Point (2.0,1.0);

        for (int i = 0; i < vertices.length; i++) {

        vertices[i].translate(5.0,7.5);

        }
    }
}

点:

package k;

public class Point {

    private double x;
    private double y;

    public Point(double x, double y){

        this.x = x;

        this.y = y;

    }

    public double getX(){

        return x;

    }

    public String toString(){

        return "("+ this.x+", "+this.y+")";
    }

    public Point copy(){

        Point copia = new Point(x, y);

        return copia;

    }


     public static double distance(Point ponto1, Point ponto2){

         double distX = ponto1.x + ponto2.x;

         double distY = ponto1.y + ponto2.y;

         return Math.sqrt(distX*distX + distY*distY);

     }

     public void translate(double dx, double dy){

         double transX = this.x + dx;

         double transY = this.y + dy;

         System.out.println(toString()+" fica "+ transX +" e "+ transY);

     }

     public static void main(String[] args) {

         Point ponto1 = new Point(2,1);

         Point ponto2 = new Point(3,9);

         System.out.println(ponto1.toString());

         System.out.println(ponto2.toString());

         System.out.println("A distancia entre os pontos eh: " +distance(ponto1, ponto2));

         System.out.println("O ponto copiado eh: "+ponto2.copy());

         ponto2.copy().translate(2, 3);

    }

}

多边形:

package k;

public class Polygon {

    private Point [] vertices;

    public Polygon(Point [] vertices){

        this.vertices = vertices;

    }

    public String toString(){

        return "Ponto 1 em: "+vertices;

    }

    public Point closestToOrigin(){

        Point proximo = new Point(0, 0);
        Point origem = new Point(0,0);

        for (int i = 0; i < vertices.length; i++) {

            double distancia = Point.distance(vertices[i], origem);

            if(distancia<Point.distance(vertices[i-1], origem)){

                proximo = vertices[i];

            }

        }

        return proximo;

    }

    public void translate(double dx, double dy){

        Point Trans = new Point(dx, dy);

        System.out.println("Ponto 1 fica: "+vertices[0]+Trans+", Ponto 2 fica: "+vertices[1]+Trans+" e Ponto 3 fica: "+vertices[2]+Trans);

    }

}

2 个答案:

答案 0 :(得分:0)

在调用非静态方法之前,必须实例化Polygon类:

Polygon poly = new Polygon(vertices);

然后你可以这样做:

poly.translate(5.0,7.5);

答案 1 :(得分:0)

要调用非静态方法,您需要通过执行

来实例化另一个类
Class object = new Class(parameters);

然后在该对象上调用该方法:

object.method();