Java 次要非公共类使用会产生错误“类型不可见”即使访问的方法在主类中是公共的
我有一个 Main.java 文件:
public class Main{
private EntityDrawer entityDrawer;
public void setEntityDrawer(EntityDrawer entityDrawer) {
this.entityDrawer = entityDrawer;
}
public EntityDrawer getEntityDrawer() {
return entityDrawer;
}
}
class EntityDrawer {
private Empleado empleado;
public Empleado getEmpleado() {
return empleado;
}
public void setEmpleado(Empleado empleado) {
this.empleado = empleado;
}
}
如果我尝试从另一个文件访问,如果我只尝试访问实体管理器,它就可以工作:
Main main = new Main();
main.getEntityDrawer(); // NO PROBLEM!
但是如果我尝试从实体管理器访问其中一个属性(即使是公共的),它就不起作用:
Main main = new Main();
main.getEntityDrawer().getEmpleado(); // Gives error "The type EntityDrawer is not visible"
我不明白为什么会发生这种情况,有人可以给我一些关于这个问题的见解吗?...
I have a Main.java file:
public class Main{
private EntityDrawer entityDrawer;
public void setEntityDrawer(EntityDrawer entityDrawer) {
this.entityDrawer = entityDrawer;
}
public EntityDrawer getEntityDrawer() {
return entityDrawer;
}
}
class EntityDrawer {
private Empleado empleado;
public Empleado getEmpleado() {
return empleado;
}
public void setEmpleado(Empleado empleado) {
this.empleado = empleado;
}
}
If I try to access from another file, it works if I only try to access the entityManager:
Main main = new Main();
main.getEntityDrawer(); // NO PROBLEM!
But if I try to access one of the attributes (even if public) from entityManager, it does not work:
Main main = new Main();
main.getEntityDrawer().getEmpleado(); // Gives error "The type EntityDrawer is not visible"
I cannot understand why is happening, could someone give me some insight into this issue?...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您正在尝试在另一个包中使用包本地类 EntityDrawer ,但您不能这样做。
尝试将类设为
public
I assume you are trying to use a package local class
EntityDrawer
in another package, which you cannot do.Try making the class
public
将类设为
public
或将调用类移动到同一个包中。Make the class
public
or move the calling class to same package.我也对这个问题感到恼火,我删除了不必要的 jar 文件,只在类路径中添加所需的 jar 文件。有时,如果您将多余的 jar 文件放在类路径中,会导致 jar 文件发生冲突,从而显示错误(例如“类型 org.apache.lucene.index.DirectoryReader 不可见”) )。
谢谢。
I too got annoyed with this problem, I removed unnecessary jar files and add only required jar files in the classpath. Sometimes if you put redundant jar files in the class path will leads to conflicting of the jar files and that will shows the error(like "the type org.apache.lucene.index.DirectoryReader is not visible").
Thank you.