首页 > 解决方案 > 为什么这个导入不能跨 Eclipse 中的包工作?

问题描述

这是项目架构: 在此处输入图像描述

这是无法编译的 Car 类:

package car;

import engine.Engine;

    public class Car {
        private Engine engine;

        /***
         * Constructor
         * @param engine The engine that the car object will have
         */
        Car(Engine engine) {
            engine = new Engine(engine);    // Use the Engine copy constructor
        }
    }

这是错误: 在此处输入图像描述

这是引擎包中的引擎类:

package engine;

public class Engine {
    private String name;
    private int displacement;

    public Engine(String name, int displacement) throws Exception {
        setName(name);
        setDisplacement(displacement);
    }
    public String getName() {return name;}
    public int getDisplacement() {return displacement;}

    private void setName(String name) throws Exception {
        if (name.trim().length() == 0) {
            throw new Exception("Engine name cannot be blank");
        }
    }
    private void setDisplacement(int displacement) throws Exception {
        if (displacement < 0) {
            throw new Exception("Engine displacement cannot be zero or negative");
        }
    }
}

标签: javaeclipse

解决方案


修复:我没有更改代码,我从工作区中删除了项目,关闭了 Eclipse,用不同的工作区重新打开了 Eclipse,并将相同的项目导入到工作区中。啊。


推荐阅读