我只得到了一个 .class 文件作为作业,并且需要编写一个访问其方法的程序。好吧,我在同一目录中有 .class 文件,但我无法访问它的方法,所以我的程序无法编译!我尝试过 JGrasp 和 Eclipse。两者都“找不到符号”(方法名称)。
我绝对确信我使用了正确的方法名称。为什么不起作用?
这是我在 Hw.java 中的代码
public class Hw {
public static void main(String[] args)
{
int[] a1 = {1, 2, 3, 4, 5};
int[] a2 = {5, 4, 3, 2, 1};
int[] a3 = {1};
int[] a4 = {2, 5, 3, 1, 4};
int[] a5 = {1, 2, 1};
System.out.println(sortA(a1));
System.out.println(sortA(a2));
System.out.println(sortA(a3));
System.out.println(sortA(a4));
System.out.println(sortA(a5));
}
}
我在 JGrasp 中遇到的错误:
Hw.java:11: 错误: 找不到符号
System.out.println(sortA(a1));
^
符号:方法 sortA(int[])
位置:Hw 类
最佳答案
如果 sortA
是给定类的静态方法,则必须使用类名对其进行限定:
System.out.println(GivenClass.sortA(a1));
关于即使类文件位于同一目录中,Java 文件也无法访问方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15038617/