Static Import

http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html
http://java.sun.com/developer/JDCTechTips/2004/tt1005.html

下面是对使用Static Import的一条良好的建议:
So when should you use static import? Very sparingly! Only use it when you'd otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). In other words, use it when you require frequent access to static members from one or two classes. If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import. Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from. Importing all of the static members from a class can be particularly harmful to readability; if you need only one or two members, import them individually. Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names.

在 Java 开发中,特别是在使用 IntelliJ IDEA 等 IDE 时,设置 `Class count to use import with '*'` 和 `Names count to use static import with '*'` 是为了控制自动导入的策略。这些设置决定了在导入多个类或静态成员时,IDE 是否会将多个 `import` 语句替换为通配符 `*` 的形式。 ### `Class count to use import with '*'` 该设置用于控制普通类导入时的行为。如果从同一个包中导入的类数量超过该设置的值,则 IDE 会自动将这些类的导入语句替换为通配符 `*` 的形式。例如,当设置为 99 时,如果导入的类数量超过 99 个,IDE 会将这些 `import` 语句合并为 `import package.*;` 的形式[^1]。 ### `Names count to use static import with '*'` 该设置用于控制静态导入(`static import`)的行为。如果从同一个类中静态导入的成员数量超过该设置的值,则 IDE 会自动将这些静态导入语句替换为通配符 `*` 的形式。例如,当设置为 99 时,如果静态导入的成员数量超过 99 个,IDE 会将这些 `import` 语句合并为 `import static package.Class.*;` 的形式[^1]。 ### 示例 假设有一个类需要从 `java.util` 包中导入多个类,例如: ```java import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.TreeMap; ``` 如果 `Class count to use import with '*'` 设置为 5,并且导入的类数量超过该值,则 IDE 会将其自动替换为: ```java import java.util.*; ``` 同样,对于静态导入,假设需要从 `java.lang.Math` 类中导入多个静态方法: ```java import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; ``` 如果 `Names count to use static import with '*'` 设置为 3,并且导入的静态成员数量超过该值,则 IDE 会将其自动替换为: ```java import static java.lang.Math.*; ``` ### 总结 - `Class count to use import with '*'` 控制普通类导入的通配符行为。 - `Names count to use static import with '*'` 控制静态导入的通配符行为。 - 这些设置有助于减少代码中冗余的 `import` 语句,使代码更简洁。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值