java - java程序中的"Cannot find symbol"

标签 java classpath javac

所以这是一个困境: 我正在使用两个不同的类和两个不同的包编写一个程序。然而,当我尝试从 BaseGame 调用方法(具有该方法的类)时。我检查了拼写,但我很困惑。

代码如下:

来自 Automobile.java:

package mygame.model;

import com.jme3.bullet.BulletAppState;
import com.jme3.app.SimpleApplication;
import com.jme3.bounding.BoundingBox;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.collision.shapes.MeshCollisionShape;
import com.jme3.bullet.control.VehicleControl;
import com.jme3.bullet.nodes.PhysicsNode;
import com.jme3.bullet.objects.VehicleWheel;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.FastMath;
import com.jme3.math.Matrix3f;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.shadow.BasicShadowRenderer;
import com.jme3.texture.Texture.WrapMode;
import mygame.BaseGame;

public class Automobile extends Model {
    float stiffness;
    float compValue;
    float dampValue;
    float mass;
    Node node;
    VehicleControl vehicle;
    float wheelRadius;

    // <editor-fold defaultstate="collapsed" desc="findGeom">
private Geometry findGeom(Spatial spatial, String name) {
    if (spatial instanceof Node) {
        Node node = (Node) spatial;
        for (int i = 0; i < node.getQuantity(); i++) {
            Spatial child = node.getChild(i);
            Geometry result = findGeom(child, name);
            if (result != null) {
                return result;
            }
        }
    } else if (spatial instanceof Geometry) {
        if (spatial.getName().startsWith(name)) {
            return (Geometry) spatial;
        }
    }
    return null;
}// </editor-fold>
    private void buildVehicle() {
    //Load model and get chassis Geometry
    node = (Node)loadModel("Models/Ferrari/Car.scene");
    node.setShadowMode(ShadowMode.Cast);
    Geometry chasis = findGeom(node, "Car");
    BoundingBox box = (BoundingBox) chasis.getModelBound();

    //Create a hull collision shape for the chassis
    CollisionShape carHull = CollisionShapeFactory.createDynamicMeshShape(chasis);

    //Create a vehicle control
    vehicle = new VehicleControl(carHull, mass);
    node.addControl(vehicle);

    //Setting default values for wheels
    vehicle.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));
    vehicle.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));
    vehicle.setSuspensionStiffness(stiffness);
    vehicle.setMaxSuspensionForce(10000);

    //Create four wheels and add them at their locations
    //note that our fancy car actually goes backwards..
    Vector3f wheelDirection = new Vector3f(0, 1, 0);
    Vector3f wheelAxle = new Vector3f(-1, 0, 0);

    Geometry wheel_fr = findGeom(node, "WheelFrontRight");
    wheel_fr.center();
    box = (BoundingBox) wheel_fr.getModelBound();
    wheelRadius = box.getYExtent();
    float back_wheel_h = (wheelRadius * 1.7f) - 1f;
    float front_wheel_h = (wheelRadius * 1.9f) - 1f;
    vehicle.addWheel(wheel_fr.getParent(), box.getCenter().add(0, -front_wheel_h, 0),
            wheelDirection, wheelAxle, 0.2f, wheelRadius, true);

    Geometry wheel_fl = findGeom(node, "WheelFrontLeft");
    wheel_fl.center();
    box = (BoundingBox) wheel_fl.getModelBound();
    vehicle.addWheel(wheel_fl.getParent(), box.getCenter().add(0, -front_wheel_h, 0),
            wheelDirection, wheelAxle, 0.2f, wheelRadius, true);

    Geometry wheel_br = findGeom(node, "WheelBackRight");
    wheel_br.center();
    box = (BoundingBox) wheel_br.getModelBound();
    vehicle.addWheel(wheel_br.getParent(), box.getCenter().add(0, -back_wheel_h, 0),
            wheelDirection, wheelAxle, 0.2f, wheelRadius, false);

    Geometry wheel_bl = findGeom(node, "WheelBackLeft");
    wheel_bl.center();
    box = (BoundingBox) wheel_bl.getModelBound();
    vehicle.addWheel(wheel_bl.getParent(), box.getCenter().add(0, -back_wheel_h, 0),
            wheelDirection, wheelAxle, 0.2f, wheelRadius, false);

    vehicle.getWheel(2).setFrictionSlip(4);
    vehicle.getWheel(3).setFrictionSlip(4);

}
public Automobile(Spatial sp){
    super(sp);
}
}

来自 BaseGame.java

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetManager;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.Savable;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import java.io.IOException;

public class BaseGame extends SimpleApplication{

public static void main(String[] args) {
    BaseGame app = new BaseGame();
    app.start();
}
public Spatial loadModel(String asset){
    return assetManager.loadModel(asset);
}
@Override
public void simpleInitApp() {

}

@Override
public void simpleUpdate(float tpf) {
    //TODO: add update code
}

@Override
public void simpleRender(RenderManager rm) {
    //TODO: add render code
}

}

*编辑 错误如下:

E:\Game\BasicGame\src\mygame\model\Automobile.java:64: cannot find symbol
symbol  : method loadModel(java.lang.String)
location: class mygame.model.Automobile
    node = (Node)loadModel("Models/Ferrari/Car.scene");

而且,我确实导入了该类。

最佳答案

loadModel 是一个非静态方法。您需要从类的 object 实例中调用它。

此外,即使它是静态方法,您也应该从其类中调用它,例如 BaseGame.loadModel("somestring");

在您编辑答案后,我认为这显然是问题所在:

  • 您在 main (BaseGame app = new BaseGame();) 中实例化了一个 BaseGame 对象,但您似乎没有为其实现任何特殊功能。
  • 您有一个单独的类,位于单独的包中,它没有对 BaseGame 实例的引用,因为没有人向它传递这样的引用,并且 import不是实例化。

关于java - java程序中的"Cannot find symbol",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5734005/

相关文章:

java - ResultSet 不可更新,即使其设置为 CONCUR_UPDATABLE

java - 从类路径访问资源文件

Java 访问被拒绝

具有递归类型参数的 Java 泛型边界

java - 以 HashSet 为值迭代 HashMap

java - 替换特定数字 - For 循环

java - 二维图像中的 Gabor 小波

java - 关于 Java 类路径,需要澄清

java - 哪个存储库有 eXist 以及如何使用 gradle 将其添加到类路径中?

java - 使用javac编译时的问题