java 从另一个项目导入一个类或在当前项目中创建一个类 (NetBeans)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10616982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Import a class from another project or make a class in current project (NetBeans)
提问by foki
Yesterday I have started learning Java tutorials from Oracle site and I have a problem with the first program (Bicycle :). I have made project with only one class (class Bicycle
), then open new project and wrote the class that creates two Bicycle
objects and invokes their methods. When I try to build project receive error message:
昨天我开始从 Oracle 网站学习 Java 教程,但第一个程序(自行车 :)有问题。我只创建了一个类 ( class Bicycle
) 的项目,然后打开新项目并编写了创建两个Bicycle
对象并调用它们的方法的类。当我尝试构建项目时收到错误消息:
"error: cannot find symbol
Bicycle bike1=new Bicycle();
symbol: class Bicycle
location: class BicycleDemo"
.
"error: cannot find symbol
Bicycle bike1=new Bicycle();
symbol: class Bicycle
location: class BicycleDemo"
.
I tried right click on Libraries and add Project - didn't work, tried to create new class in current project (with same content) - didn't work. What to do?
我尝试右键单击库并添加项目 - 没有用,尝试在当前项目中创建新类(具有相同的内容) - 没有用。该怎么办?
package bicycledemo;
/**
*
* App witch simulates using of Bicyle class.
*/
public class BicycleDemo {
import Bicycle;
public static void main(String[] args) {
Bicycle bike1=new Bicycle();
Bicycle bike2=new Bicycle();
bike1.changeCadence(34);
bike1.increaseSpeed(3);
bike1.changeGear(2);
bike1.printStates();
bike2.changeCadence(3);
bike2.increseSpeed(12);
bike2.printStates();
}
}
And I also have whole C:\Users\nojo\Documents\NetBeansProjects\Bicycle file in Libraries of project BicycleDemo. Code of Bicycle.java:
而且我在 BicycleDemo 项目的库中还有整个 C:\Users\nojo\Documents\NetBeansProjects\Bicycle 文件。Bicycle.java 的代码:
public class Bicycle {
int cadence=0;
int speed=0;
int gear=1;
void changeCadence(int newValue){
cadence=newValue;
}
void increaseSpeed(int increase){
speed=speed+increase;
}
void applyBreaks(int decrease){
speed=speed-decrease;
}
void changeGear(int gearNumber){
gear=gearNumber;
}
void printStates(){
System.out.println("cadence:" + cadence + "speed:" + speed +
"gear:" + gear);
}
}
回答by ChadNC
Looks like your import statement is in the wrong location. It should be below the package name and before the the beginning of the clas definition.
看起来您的导入语句位置错误。它应该在包名之下和类定义的开始之前。
package bicycledemo;
import <yourpackagename>.Bicycle;
You can do this where you are currently declaring bike1 but you have to use thepackage name and class name when you do.
您可以在当前声明自行车 1 的地方执行此操作,但在执行此操作时必须使用包名和类名。
<yourpackagename>.Bicycle bike1 = new Bicycle();
What you're reading is a tutorial on the "concepts" of OO programming and not an in depth tutorial, Packages are explained further along in the tutorial.
您正在阅读的是关于 OO 编程“概念”的教程,而不是深入的教程,教程中将进一步解释包。
Your problem is probably you made two projects, one has the bicycle class and one has the bicycledemo class, correct? If that is correct then in both projects your class is in the default package, which is bad. To fix your problem, create a new project with both classes in the same project.
你的问题可能是你做了两个项目,一个是自行车课,一个是自行车演示课,对吗?如果这是正确的,那么在两个项目中,您的课程都在默认包中,这很糟糕。要解决您的问题,请在同一个项目中创建一个包含两个类的新项目。