【问题标题】:How to call non-static method from another class? [duplicate]如何从另一个类调用非静态方法? [复制]
【发布时间】:2017-01-13 18:31:07
【问题描述】:

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

客户端多边形:

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

    }

}

【问题讨论】:

  • 非静态方法只能从另一个类调用,前提是你有一个类的实例和你要调用的方法。
  • “具有非静态方法的 Point 数组”“当我运行 ClientePolygon 时,它会执行 Point 类” - 这些语句不会没有意义,似乎表明对基本概念的误解。你需要澄清你在问什么。
  • 您想在 什么 上“运行 Polygon”,究竟是什么?
  • 我不知道为什么,但是如果我运行 ClientePolygon,它会运行另一个程序。如果我穿上 cmets(另一个程序),当我运行 ClientePolygon 时,它说他找不到另一个程序的主类。奇怪...

标签: java object methods


【解决方案1】:

要调用非静态方法,您需要实例化另一个类

Class object = new Class(parameters);

然后调用该对象的方法:

object.method();

【讨论】:

    【解决方案2】:

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

    Polygon poly = new Polygon(vertices);
    

    那么你可以这样做:

    poly.translate(5.0,7.5);
    

    【讨论】:

      猜你喜欢
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 2017-02-25
      相关资源
      最近更新 更多