在Java中使用默认包导入类的语法是什么?

编程入门 行业动态 更新时间:2024-10-11 01:12:42
本文介绍了在Java中使用默认包导入类的语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能重复:如何访问默认包中的java-classes?

是可以在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中使用默认包导入类的语法是什么?

本文发布于:2023-11-27 22:40:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639894.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语法   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!