在Java中使用默认包导入类的语法是什么? [英] What's the syntax to import a class in a default package in Java?
本文介绍了在Java中使用默认包导入类的语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是可以在Java中导入默认包中的类吗?如果是这样,语法是什么?例如,如果您有
Is it possible to import a class in Java which is in the default package? If so, what is the syntax? For example, if you have
package foo.bar;
public class SomeClass {
// ...
在一个文件中,你可以写
in one file, you can write
package baz.fonz;
import foo.bar.SomeClass;
public class AnotherClass {
SomeClass sc = new SomeClass();
// ...
在另一个文件中。但是,如果SomeClass.java不包含包声明呢?你如何在 AnotherClass
中引用 SomeClass
?
in another file. But what if SomeClass.java does not contain a package declaration? How would you refer to SomeClass
in AnotherClass
?
推荐答案
您无法从默认包中导入类。除了非常小的示例程序之外,您应该避免使用默认包。
You can't import classes from the default package. You should avoid using the default package except for very small example programs.
来自 Java语言规范:
它是从
未命名包导入类型时编译
时间错误。
It is a compile time error to import a type from the unnamed package.
这篇关于在Java中使用默认包导入类的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文