如何使用其他程序包中的方法?

时间:2019-01-05 13:04:35

标签: java oop netbeans import

我需要从与我正在研究的类不同的包中访问类,但是我无法使其正常工作。我目前正在PMSModel包中处理ConcreteAccount,需要从Account.Strategies中的类访问方法。

尝试执行此操作时,我不断收到“找不到符号”错误。我有另一个项目使用与我尝试使用的方法完全相同的方法,并且没有错误。当类具有构造函数时,我尝试访问它们,但是当类没有构造函数时,却得到了完全相同的错误。我有以下错误的图片:

error being thrown

下面是我尝试从Account.Strategies包中访问类的代码。错误本身显示在语句的“策略”部分。

package PMSModel;

import Account.Strategies.IAccountStrategy;

public class ConcreteAccount extends Account {

@Override
public IAccountStrategy selectStrategy(String accountType){
    IAccountStrategy strategy;

    switch(accountType){
        case "Patient":
            strategy = new Account.Strategies.PatientStrategy();
            break;
        case "Secretary":
            strategy = new Account.Strategies.SecretaryStrategy();
            break;
    }
    return strategy;
}
}

下面是我的包和类如何相互关联的屏幕截图:

project file area

我希望“策略”存储该类,以便我可以在Account中访问该特定类的方法。

1 个答案:

答案 0 :(得分:1)

首先,必须使用小写形式来编写软件包命名的良好做法。 其次,您面临的问题是因为您没有一个根包,例如,您的项目应该共享该根包:

com.example.projectname

这应该是您的根,并在此启动其他软件包

com.example.projectname.account.strategies
com.example.projectname.controller
com.example.projectname.guiview
com.example.projectname.pmsmodel
com.example.projectname.resources

仅供参考,尼桑(JB Nizet)打给你的方式是我同意的。

参考

Java代码约定-> https://www.oracle.com/technetwork/java/codeconventions-150003.pdf