如何在Java中从main调用静态方法

问题描述

我正在尝试实现下一个代码,以将矩阵与点或向量相乘。但是我必须使用静态时间方法(强制性静态)

package reto3;

public class Matrix3x3 {
    public double matrix[][];

    public Matrix3x3(double matrix[][]) {
        this.matrix = matrix;
    }

    public Matrix3x3() {
        matrix = new double[3][3];
    }

    public static Point3 times(Matrix3x3 m3,Point3 p3) {
        double p[] = {p3.x,p3.y,p3.w};
        double result[] = new double[3];
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result.length; j++) {
                result[i] = m3.matrix[i][j] * p[j];
            }
        }
        
        return new Point3(result[0],result[1],result[2]);
    }
}
package reto3;

public class Point3 {
    
    public double point[];
    double x;
    double y;
    double w;
    
    public Point3(double x,double y,double w){
        point = new double[3];
        point[0] = x;
        point[1] = y;
        point[2] = w;
        this.x = x;
        this.y = y;
        this.w = w;
    }

    public double[] getPoint() {
        return point;
    }

    public double getX() {
        return point[0];
    }
    
    public double getY() {
        return point[1];
    }

    public double getW() {
        return point[2];
    }

    public void setX(double x) {
        point[0] = x;
    }

    public void setY(double y) {
        point[1] = y;
    }

    public void setW(double w) {
        point[2] = w;
    }
}
package reto3;

public class Main {
    public static void main(String[] args) throws Exception {
        Matrix3x3 matrix = new Matrix3x3();
        double x = 1.5;
        double y = 2.0;
        double w = 3.5;
        Point3 point3 = new Point3(x,y,w);
        Matrix3x3.times(matrix,point3);
        
    }
}

当我编译代码时,它向我显示以下内容

src/reto3/Main.java:5: error: cannot find symbol
        Matrix3x3 matrix = new Matrix3x3();
        ^
  symbol:   class Matrix3x3
  location: class Main
src/reto3/Main.java:5: error: cannot find symbol
        Matrix3x3 matrix = new Matrix3x3();
                               ^
  symbol:   class Matrix3x3
  location: class Main
src/reto3/Main.java:9: error: cannot find symbol
        Point3 point3 = new Point3(x,w);
        ^
  symbol:   class Point3
  location: class Main
src/reto3/Main.java:9: error: cannot find symbol
        Point3 point3 = new Point3(x,w);
                            ^
  symbol:   class Point3
  location: class Main
src/reto3/Main.java:10: error: cannot find symbol
        Matrix3x3.times(matrix,point3);
        ^
  symbol:   variable Matrix3x3
  location: class Main
5 errors

我不确定我是否正确调用了该方法,或者应该如何使用静态方法来实现它。

感谢您的帮助

解决方法

您需要导入所有外部类,以便可以对其进行访问。

因此,将point3导入matrix3x3类, 在主类中导入point3和matrix3x3

使用诸如Intellij之类的思想进行编码。他们将自动处理此问题。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...