无法从另一个包访问自定义包方法
我有 1 个用户定义包名称 pack
,在这个包内部有类名称 abc
,在这个包之外我有另一个类名称是 xyz
。
我的文件层次结构:
--> new (Folder Name)
--> pack (Root Directory)
* abc.class
--> abc.java (Outside of package)
--> xyz.java (Outside of package)
--> xyz.class (Outside of package)
abc 文件代码
package pack;
public class abc{
public abc(){
System.out.println("hello");
}
public void sum(){
System.out.println("hello i am method");
}
}
xyz 文件代码
import pack.abc;
public class xyz{
public static void main(String[] args){
abc obj = new abc();
}
}
在此代码中,当我在 xyz
文件中仅创建对象(意味着仅调用构造函数)时,我会得到输出,但是当我调用 sum() 使用对象的方法,然后我收到错误
未找到符号
。
以下代码用于调用 sum()
方法:
import pack.abc;
public class xyz{
public static void main(String[] args){
abc obj = new abc();
obj.sum();
}
}
错误:
error: cannot find symbol
obj.sum();
^
symbol: method sum()
location: variable obj of type abc
I have 1 user defind package name pack
and inside this pakage have class name abc
and outside of this package i have another class name is xyz
.
My File Hierarchy:
--> new (Folder Name)
--> pack (Root Directory)
* abc.class
--> abc.java (Outside of package)
--> xyz.java (Outside of package)
--> xyz.class (Outside of package)
abc file code
package pack;
public class abc{
public abc(){
System.out.println("hello");
}
public void sum(){
System.out.println("hello i am method");
}
}
xyz file code
import pack.abc;
public class xyz{
public static void main(String[] args){
abc obj = new abc();
}
}
In this code when i create only Object (Means only call Constructor) in xyz
file then i get output but when i call sum()
method using object then i get error Symbol not found
.
Following code for calling sum()
method :
import pack.abc;
public class xyz{
public static void main(String[] args){
abc obj = new abc();
obj.sum();
}
}
Error :
error: cannot find symbol
obj.sum();
^
symbol: method sum()
location: variable obj of type abc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论