【发布时间】:2020-12-17 12:58:40
【问题描述】:
我正在尝试实现下一个代码以将矩阵与点或向量相乘。但我必须使用静态时间方法(强制静态)
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, y, w);
^
symbol: class Point3
location: class Main
src/reto3/Main.java:9: error: cannot find symbol
Point3 point3 = new Point3(x, y, 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
我不确定我是否正确调用了该方法,或者它应该如何使用静态方法来实现。
感谢您的帮助
【问题讨论】:
-
问题不在于调用静态方法,而是编译器在
Main中找不到您尝试使用的其他类 -
“当我编译代码时”你是如何编译这些类的?
-
你的项目的包结构是什么?
Matrix3x3需要导入吗?是进口的吗? -
包是reto3,我用
javac Main.java编译代码。 -
我不确定那个“不”是否适合我,但无论如何要运行它,一旦一切都编译好,你只需要
java Main