[英]how do you call a method from a different file in java?
這是我的兩個文件:文件 1:
public class Step2 {
public static void main(String[] args) {
output("-STEP 2-");lineBreak();
int size = inputINT("How many smart devices do you have: ");
SmartHome[] sh = new SmartHome[size];
for(int i = 0;i<size;i++){
SmartDevice insertValues = new SmartDevice(inputSTR(
"Device name " + (i+1)),
inputDBL("Device location " + (i+1)),
inputBLN("Device status " + (i+1)));
insertDevice(insertValues);
}
文件 2:
public class SmartHome {
private SmartDevice[] smartDevices;
public SmartHome(int size){
SmartDevice[] smartDevices = new SmartDevice[size];
}
public SmartHome(SmartDevice[] sd){enter code here
smartDevices = sd;
}
public void insertDevice(SmartDevice device){
for (int i=0;i<smartDevices.length;i++) {
if (smartDevices[i] == null)
smartDevices[i] = device;
break;
}
}
(這兩個文件在同一個項目中)當我嘗試調用 insertDevice 方法時,出現“找不到符號”的問題,我不確定為什么。 我試過調用文件 2 中存在的其他方法,但是我遇到了同樣的問題,因此,編譯器似乎在錯誤的地方查找? 請幫忙
好吧,您正在創建對象數組sh = new SmartHome
這樣您就可以通過簡單地調用對象內的方法sh[index].insertDevice(device);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.