gpt4 book ai didi

java - 在 Java 中对导入进行分组

转载 作者:行者123 更新时间:2023-12-02 22:12:56 25 4
gpt4 key购买 nike

Java 是否支持对导入进行分组,如下所示:

import package.{Class1,Class2}

我知道 * 运算符导入子包,但我想导入特定的子包。

<小时/>

Rust 或一些现代语言中,它的支持如下:

use package::{Class1, Class2};

除了像这样逐行编写每个导入之外,还有其他选择吗?

import package.Class1;
import package.Class2;

最佳答案

Java 语言规范,第 7.5. Import Declarations 部分,显示:

An import declaration allows a named type or a static member to be referred to by a simple name (§6.2) that consists of a single identifier.

[...]

  • A single-type-import declaration (§7.5.1) imports a single named type, by mentioning its canonical name (§6.7).

  • A type-import-on-demand declaration (§7.5.2) imports all the accessible types of a named type or named package as needed, by mentioning the canonical name of a type or package.

  • A single-static-import declaration (§7.5.3) imports all accessible static members with a given name from a type, by giving its canonical name.

  • A static-import-on-demand declaration (§7.5.4) imports all accessible static members of a named type as needed, by mentioning the canonical name of a type.

如您所见,它要么是单一命名类型,要么是所有可访问类型。没有类型列表的语法。

<小时/>

旁注:我几乎从不查看代码的 import 语句。我让 Eclipse 帮我管理(Source > Organize Imports... (Ctrl+Shift+O)),所以我并不关心有很多单一类型的导入语句。无论如何,包含导入的部分已折叠,因此我什至不必滚动它们。噢,使用优秀 IDE 的乐趣

关于java - 在 Java 中对导入进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58637812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com