我需要从我正在处理的类中的一个单独的包中访问类,但是我无法让它工作。我目前正在 PMSModel 包中的 ConcreteAccount 上工作,需要访问 Account.Strategies 中的类中的方法。
尝试执行此操作时,我不断收到“找不到符号”错误。我有另一个项目,它使用与我尝试使用的完全相同的方法,并且没有错误。我尝试在类有构造函数时和没有构造函数时访问它们,但我得到了完全相同的错误。我有以下错误的图像:
下面是我尝试从 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;
}
}
下面是我的包和类如何相互关联的屏幕截图:
我希望“策略”存储该类,以便我可以访问 Account 中该特定类的方法。
最佳答案
首先,命名包的良好做法必须使用小写字母。 其次,您面临的问题是因为您没有应该由项目共享的根包,例如:
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
关于java - 如何使用不同包中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54052290/