gpt4 book ai didi

java - 当我已经声明了该方法时,获取类中未找到的方法?

转载 作者:行者123 更新时间:2023-11-30 06:22:32 24 4
gpt4 key购买 nike

我正在尝试创建主方法中的类的对象,我正在实现惰性简单模式,但我不断收到错误“无法在类中找到符号”。我检查了导入包语句是否正确编写。

这是我的主课

package control;
import java.io.File;
import java.io.FileNotFoundException;
import model.ApplicationModel;
import java.util.* ;
import model.Shop;
import view.ApplicationViewer;
import model.ApplicationModel;

public class ApplicationControl {
public static void main (String[] args) throws FileNotFoundException{

ApplicationModel apm = new ApplicationModel.getInstance();

}

}

这是我的 Singleton 类 ApplicationModel

package model;
// needed for ArrayLists
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ApplicationModel {
private static ApplicationModel instance = null;

private ApplicationModel()
{

}
public static ApplicationModel getInstance (){
if (instance == null){
instance = new ApplicationModel();
}
return instance;
}

private List<Shop> shops = new ArrayList<Shop>();
public List<Shop> getShops(){
return this.shops;
}

public void setShops(List<Shop> shops){
this.shops = shops;
}

public Shop createShop(String csvString){
String[] attributes = csvString.split(",");
Shop shop = new Shop(attributes[0],attributes[1],attributes[2],
attributes[3],attributes[4]);
return shop;
}

public List<Shop> readShops(String shopFileName){

ApplicationModel am = new ApplicationModel();
List<Shop> shopList = new ArrayList<>();
try{
Scanner naughty = new Scanner(new File(shopFileName));
if (naughty.hasNext()) naughty.nextLine();
while(naughty.hasNext()){
shopList.add(am.createShop(naughty.nextLine()));
}
} catch (FileNotFoundException ex) {
Logger.getLogger(ApplicationModel.class.getName()).log(Level.SEVERE, null, ex);
}
return shopList;
}

public String printShops(){
String listOfShops ="";
for(Shop shop : shops ){
listOfShops = listOfShops +'\n'+ shop.toString().trim() + '\n';
}
return listOfShops.trim();
}
}

每当我在主类中输入 ApplicationModel 时,指出尚未使用导入的导入语句错误也会消失,我不确定出了什么问题(我正在使用 netbeans)。有人可以帮忙吗?

最佳答案

从代码中删除“new”:

ApplicationModel apm = ApplicationModel.getInstance();

作为静态getInstance()是一个方法(不是实例方法)。此语法是您调用类方法的方式。

关于java - 当我已经声明了该方法时,获取类中未找到的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47838177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com