当我已经声明了方法时,获取在类中找不到的方法?

时间:2017-12-15 18:43:13

标签: java netbeans

我正在尝试在main方法中创建我所拥有的类的对象,我正在实现懒惰的simpleton模式,但我不断得到错误,无法在类中找到符号。我已经检查过我是否也正确编写了导入包语句。

这是我的主要课程

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)。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

从代码中删除“new”:

ApplicationModel apm = ApplicationModel.getInstance();

成为staticgetInstance()方法(不是实例方法)。这种语法就是你调用类方法的方法。