无法解析为类型?

时间:2021-12-16 23:53:05

I have a service interface that reads thus

我有一个这样的服务接口。

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;

}

And a factory that looks like this...

像这样的工厂…

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);
}
}

This should be really simple - but I keep getting two errors - IService cannot be resolved to a type (on both the factory and the service) and also an serviceName cannot be resolved into a variable error in the factory. I know I am missing something simple...

这应该非常简单——但是我一直得到两个错误——IService不能被解析为一个类型(在工厂和服务上),并且一个serviceName不能被分解为工厂中的一个变量错误。我知道我错过了一些简单的事情……

2 个解决方案

#1


1  

Regarding the type error: if IService is not in the package services, then IItemService will not have access to it and you will need to import some.package.name.IService.

关于类型错误:如果IService不在包服务中,则IItemService将无法访问它,您将需要导入some.package.iservice。

The second answer is simple: in the getService and getImplName methods, the parameter is named name. In the method bodies, you're referring to it as serviceName. You should probably change name to serviceName in each of them.

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

If, on the other hand, you're trying to access an instance variable, note that in Java you have to declare all instance variables before you can access (read from/write to) them. You would need to modify the class as such:

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

public class Factory {

    private String serviceName;

    public Factory () {}

    // other code
}

#2


1  

Well the second error is simple: there isn't a variable called serviceName. If you think there is, point to its declaration. I suspect you meant to use the parameter - in which case just change the parameter name to match the name you're trying to use:

第二个错误很简单:没有一个名为serviceName的变量。如果你认为有,那就指出它的宣言。我怀疑您是想使用参数——在这种情况下,只需更改参数名称以匹配您正在使用的名称:

public IService getService(String serviceName) throws ServiceLoadException 

...

private String getImplName (String serviceName) throws Exception

This was a pretty simple error - you should think about what bit of your diagnosis let you down in working it out for yourself. (You should also indent your code more sensibly, btw.)

这是一个非常简单的错误——你应该考虑一下你的诊断结果让你自己做了些什么。(你也应该更明智地缩进你的代码。)

As for IService being missing - we have no idea where IService is meant to be declared, which makes it very hard to help you on this front...

关于IService的缺失——我们不知道IService应该在哪里宣布,这使得在这方面帮助你非常困难……

#1


1  

Regarding the type error: if IService is not in the package services, then IItemService will not have access to it and you will need to import some.package.name.IService.

关于类型错误:如果IService不在包服务中,则IItemService将无法访问它,您将需要导入some.package.iservice。

The second answer is simple: in the getService and getImplName methods, the parameter is named name. In the method bodies, you're referring to it as serviceName. You should probably change name to serviceName in each of them.

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

If, on the other hand, you're trying to access an instance variable, note that in Java you have to declare all instance variables before you can access (read from/write to) them. You would need to modify the class as such:

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

public class Factory {

    private String serviceName;

    public Factory () {}

    // other code
}

#2


1  

Well the second error is simple: there isn't a variable called serviceName. If you think there is, point to its declaration. I suspect you meant to use the parameter - in which case just change the parameter name to match the name you're trying to use:

第二个错误很简单:没有一个名为serviceName的变量。如果你认为有,那就指出它的宣言。我怀疑您是想使用参数——在这种情况下,只需更改参数名称以匹配您正在使用的名称:

public IService getService(String serviceName) throws ServiceLoadException 

...

private String getImplName (String serviceName) throws Exception

This was a pretty simple error - you should think about what bit of your diagnosis let you down in working it out for yourself. (You should also indent your code more sensibly, btw.)

这是一个非常简单的错误——你应该考虑一下你的诊断结果让你自己做了些什么。(你也应该更明智地缩进你的代码。)

As for IService being missing - we have no idea where IService is meant to be declared, which makes it very hard to help you on this front...

关于IService的缺失——我们不知道IService应该在哪里宣布,这使得在这方面帮助你非常困难……