Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';不能解析为类型_Java - Fatal编程技术网

Java Can';不能解析为类型

Java Can';不能解析为类型,java,Java,我有一个这样的服务接口 package services; import domain.Items; public interface IItemsService extends IService { public final String NAME = "IItemsService"; /** Places items into the database */ public void storeItem(Items items); /** Retrieves items fr

我有一个这样的服务接口

package services;

import domain.Items;

public interface IItemsService extends IService {

public final String NAME = "IItemsService";


/** Places items into the database */
public void storeItem(Items items);




/** Retrieves items from the database
 * 
 * @param category
 * @param amount
 * @param color
 * @param type
 * @return
 * @throws ClassNotFoundException 
 */
public Items getItems (String category, float amount, String color, String type) throws ItemNotFoundException, ClassNotFoundException;

}
还有一个像这样的工厂

package services;
public class Factory {



public Factory(){}



@SuppressWarnings("unchecked")
public IService getService(String name) throws ServiceLoadException {
    try {
        Class c = Class.forName(getImplName(serviceName));
        return (IService)c.newInstance();
    } catch (Exception e) {
        throw new ServiceLoadException(serviceName + "not loaded");
    }
}



private String getImplName (String name) throws Exception {
    java.util.Properties props = new java.util.Properties();
        java.io.FileInputStream fis = new java.io.FileInputStream("properties.txt");
            props.load(fis);
                fis.close();
                return props.getProperty(serviceName);
}
}

这应该非常简单-但我一直收到两个错误-IService无法解析为类型(在工厂和服务上),serviceName也无法解析为工厂中的变量错误。我知道我缺少了一些简单的东西…

第二个错误很简单:没有名为
serviceName
的变量。如果你认为有,指向它的声明。我怀疑您打算使用参数-在这种情况下,只需更改参数名称以匹配您尝试使用的名称:

public IService getService(String serviceName) throws ServiceLoadException 

...

private String getImplName (String serviceName) throws Exception
这是一个非常简单的错误——你应该想想你的诊断中有哪一点让你在自己解决问题时感到失望。(顺便说一句,您还应该更合理地缩进代码。)


至于
IService
丢失-我们不知道
IService
应该在哪里声明,这使得在这方面很难帮助您……

关于类型错误:如果
IService
不在包
服务中,然后,
IItemService
将无法访问它,您需要
导入一些.package.name.IService

第二个答案很简单:在
getService
getImplName
方法中,参数名为
name
。在方法体中,您将其称为
serviceName
。您可能应该将它们中的
name
更改为
serviceName

另一方面,如果您试图访问实例变量,请注意,在Java中,您必须声明所有实例变量,然后才能访问(读/写)它们。您需要对类进行如下修改:

public class Factory {

    private String serviceName;

    public Factory () {}

    // other code
}

好吧,你给恶习下定义了吗?你没有给我们看。