Java 使用文件中的函数
好吧,确实是个愚蠢的问题,但是我在名为 x.java 的文件中定义了一些方法,该文件位于默认文件夹中,并且在同一文件夹中,我有一个名为 z.java 的文件。我想在 z 中使用 x 中定义的函数。当我尝试时,它告诉我该函数未定义。我尝试输入 import x.java;
但它说 x.java 无法解析。我在这里缺少什么?
Ok really stupid question, but I have some methods defined in a file called x.java, which is in the default folder, and in the same folder, I have a file called z.java. I want to use the functions defined in x in z. When I try, it tells me the function is undefined. I tried to put import x.java;
but it says x.java cannot be resolved. What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的描述,我敢打赌您的两个源文件很可能都在 default 包中定义了类(即,您没有明确为它们定义包)。
您无法导入默认包中的类。
建议您将类
x
放入命名包中(例如 foo.bar.x),然后您可以导入它:Based on your description, I'd bet there's a good chance both of your source files defined classes in the default package (i.e., you don't explicitly define a package for them).
You can't import a class that's in the default package.
Recommend you put your class
x
in a named package (e.g., foo.bar.x), then you can import it:如果你有一个名为 x.java 的文件,它编译为 x.class,你不需要通过以下方式导入:
但你可以
if you have a file called x.java which compiles to x.class, you don't import by doing:
but you do
我认为,如果这两个类位于同一个包中,则无需导入该类,因为该类可以在没有任何导入且没有任何错误的情况下使用。
I think, if the two classes are in same package then there is no need to import the class as that class can be used without any import and without any error.