昨天我已經開始從Oracle站點學習Java教程,並且第一個程序(Bicycle :)有問題。我只用一個類(class Bicycle
)創建了項目,然後打開新項目並編寫了創建兩個Bicycle
對象並調用其方法的類。當我嘗試構建項目時收到錯誤消息:從其他項目導入課程或在當前項目中創建課程(NetBeans)
"error: cannot find symbol Bicycle bike1=new Bicycle(); symbol: class Bicycle location: class BicycleDemo"
。
我試過右鍵單擊庫並添加項目 - 沒有工作,試圖在當前項目中創建新類(具有相同的內容) - 沒有工作。該怎麼辦?
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();
}
}
而且我也有整個C:\用戶\能條\文件\項目BicycleDemo圖書館的NetBeansProjects \自行車文件。 代碼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);
}
}
您說'Bicycle'類在另一個不是'BicycleDemo'類的項目中,並且您已經將項目添加到構建路徑中(似乎您正在使用Eclipse,這是否正確)。但是你在BicycleDemo類中使用'進口自行車'嗎?另外,如果您將代碼添加到問題中,這會有所幫助,這樣您可以獲得更有針對性的異常。 – George
請發佈您的代碼,以便您可以得到幫助。 – ChadNC
因此,你有一個單獨的項目,其中有Bicycle類,並且你想將這個類導入到這個項目中並在BicycleDemo類中使用那個類? – ChadNC