Many new language features have been introduced in the release of J2SE 5.0. Static imports is a feature that helps in the creation and use of global constants. It's one of the easiest new features to incorporate into your code. In this tip, you'll have the chance to use static imports for constants and methods. You'll start with the traditional technique of using "constant interfaces". These interfaces only exist to hold constants. These constants should be moved into utility classes, but these classes have traditionally been hard to use. The problems disappear with static imports. In addition to their use for constants, static imports can ease the calling of methods and classes. However there is a downside to using static imports in this way: it can make your code less readable. This is especially true with wild cards. It can become hard to determine where methods and attributes originate.
在J2SE5.0中介绍了许多语言特性。静态导入帮助创建并使用全局常量。这是最容易融入您的代码的特性之一。这里,您将有机会使用静态导入来引用常量和方法。您将从使用“常量接口”的传统技术开始。这些接口只是用于存放常量。这些常量可以移至工具类中,但在以往,这些类很难使用。有了静态导入,这些问题就随之消失。除了对于常量的使用外,静态导入也可以简化队类中的方法调用。但在这种情况下有一个问题,这就是会降低您的代码可读性。尤其是在使用通配符的情况下,很难辨认出方法和属性是来自于哪个类。
In the past, it was common for developers to use interfaces to store values that would be treated as global constants. Here's an example:
在过去,对于开发人员来说,使用接口来储存值作为全局常量是司空见惯的。这里有一个例子:
public interface BadIrrationalConstants {
public static final double SQRT_TWO = 1.414;
public static final double SQRT_THREE = 1.732;
}
This was particularly appealing to developers who needed more than one group of constants. For instance, a developer might need the values defined in the BadIrrationalConstants
interface as well as those in the following interface, BadTranscendentalConstants
:
对于需要不只一组常量的开发人员来说,这更是有必要的。例如,开发人员也许在把值放在接口BadIrrationalConstants
的同时,也需要在接口
BadTranscendentalConstants
中定义一些别的什么。
public interface BadTranscendentalConstants {
public static final double PI = 3.14159;
public static final double E = 2.71828;
}
It is tempting to use this technique of constant interfaces because it is easy to make the global constants available for use. You simply implement each interface containing the needed constants like this:
之所以使用常量接口这种技术,是因为可以很方便的定义全局变量。您只需要像下面这样简单地实现包含必须的常量的接口:
public class BadUseOfConstants implements
BadTranscendentalConstants, BadIrrationalConstants {
public static double sinPiOverFour() {
return SQRT_TWO / 2;
}
public static void main(String[] args) {
System.out.println("Pi is approximately " + PI);
System.out.println("The sin of Pi/4 is about " +
sinPiOverFour());
}
}
Before the J2SE 5.0 release, your only alternative was to use fully qualified names such as BadIrrationalConstants.SQRT_TWO
everywhere. This was excessively verbose and led to hacks such as BadUseOfConstants
.
在J2SE5.0发布之前,你唯一的可替换的方法就是在引用的地方使用全名称BadIrrationalConstants.SQRT_TWO
来对常量进行引用。但这确实的很冗余。
The problem with the approach above is that the constants are now part of the type BadUseOfConstants
. Any class that uses these constants adds them to its public interface. This is a restriction on further modifications of the class. That's because clients of the class might depend on the constants being present. Instead, the constants should be part of a class like this:
上面方法的问题是常量作为BadUseOfConstants
的一部分。任何使用这些常量的类都必须把他们添加自己的公共接口中。这就制约了将来对类的修改。因为客户端类可能依赖于所要使用的常量。可以作为替代的方法是把常量作为类的一部分。
package staticEx;
public class IrrationalConstants {
public static final double SQRT_TWO = 1.414;
public static final double SQRT_THREE = 1.732;
}
Before you are tempted to extend IrrationalConstants
to use SQRT_TWO and SQRT_THREE
, assume that you also need the constants defined in TranscendentalConstants
:
在您尝试扩展IrrationalConstants
来使用
SQRT_TWO
和
SQRT_THREE
前,假定您也需要在
TranscendentalConstants
中定义常量:
package staticEx;
public class TranscendentalConstants {
public static final double PI = 3.14159;
public static final double E = 2.71828;
}
Before the release of J2SE 5.0, it took quite a bit of effort to use these constants, sometimes obscuring the equations in which they were used. However, static import allows you to easily import static members into your code. For example, if you want to use the static class attribute SQRT_TWO
, you declare the following:
在J2SE5.0发布之前,使用常量要花费很多精力,有时甚至在权衡该使用哪种方法时而产生困惑。但是,静态导入能让您很轻松的把静态成员导入您的代码中去。例如,如果您想使用类的静态属性SQRT_TWO
,你可以像下面这样声名:
import static staticEx.IrrationalConstants.SQRT_TWO
at the beginning of the class in which you want to use the constant. You can then use SQRT_TWO
as you did in BadTranscendentalConstants
. Note that you need to use the keyword static
after the keyword
import. If you try the following without the static
keyword, it will not compile. Compile and run the following class, ConstantsWithStaticImport
, using a J2SE 5.0 compiler:
然后就可以像在BadTranscendentalConstants
中一样使用
SQRT_TWO
。注意,您需要在关键字
import
后使用
static
。如果你没有使用
static
关键字,程序就不会编译成功。使用
J2SE5.0编译并运行下面的类
ConstantsWithStaticImport
:
package staticEx;
import static staticEx.IrrationalConstants.SQRT_TWO;
import static staticEx.IrrationalConstants.SQRT_THREE;
import static staticEx.TranscendentalConstants.PI;
public class ConstantsWithStaticImport {
public static double sinPiOverFour() {
return SQRT_TWO / 2;
}
public static void main(String[] args) {
System.out.println("Pi is approximately " + PI);
System.out.println("The sin of Pi/4 is about " +
sinPiOverFour());
}
}
You should see the following displayed:
您会看到下面的结果:
Pi is approximately 3.14159
The sin of Pi/4 is about 0.707
You can use wildcards in the import as you might expect. In this case, you can replace the first two import statements with:
您可以在导入时使用通配符。这种情况下您可以使用下面表达式替换前两个导入:
import static staticEx.IrrationalConstants.*;
As always, being specific and avoiding the use of the wildcard helps control exactly what is being imported. It also makes things clearer for someone reading your source code.
尽可能的使用详细的导入说明并避免使用通配符,这会有助于精确地控制导入。
You can use static imports for static methods as well as for static attributes. In the class below, IrrationalConstants2
, you have both:
您也可以像使用导入静态属性那样来导入静态方法。在IrrationalConstants2
类中,你使用两种:
package staticEx;
public class IrrationalConstants2 {
public static final double SQRT_TWO = 1.414;
public static final double SQRT_THREE = 1.732;
public static double sinPiOverFour() {
return SQRT_TWO / 2;
}
}
Make them all available by using the wildcard. Now you can call the static method sinPiOverFour()
, which is defined in IrrationalConstants2
, like this:
用通配符使它们都可用。现在您可以像下面这样调用IrrationalConstants2
种的
静态方法sinPiOverFour()
:
package staticEx;
import static staticEx.IrrationalConstants2.*;
import static staticEx.TranscendentalConstants.*;
public class ConstantsWithStaticImport2 {
public static void main(String[] args) {
System.out.println("Pi is approximately " + PI);
System.out.println("The sin of Pi/4 is about " +
sinPiOverFour());
}
}
You can see however that there are already readability problems. It is not easy to determine whether sinPiOverFour()
is in the IrrationalConstants2
class or in TranscendentalConstants
. You can clarify this by replacing the wildcards in the import with explicit references to the method being called. There is less confusion when you use a class in the standard J2SE distribution such as java.lang.Math
. By using the static import, you reduce client code such as Math.PI
and Math.sin()
to PI
and sin()
.
但您能够看出可读性问题了。很难判断sinPiOverFour()
是在类
IrrationalConstants2
中,还是在
TranscendentalConstants
中。您可以显示地引用被调用方法来替换通配符。当您使用
J2SE中的java.lang.Math
时,可以降低疑惑。用静态导入您可以用
PI
和
sin()
来简化
Math.PI
和
Math.sin()
。
package staticEx;
import static java.lang.Math.*;
public class ConstantsWithStaticImport3 {
public static void main(String[] args) {
System.out.println("Pi is approximately " + PI);
System.out.println("The sin of Pi/4 is about " +
sin(PI / 4));
}
}
In addition to static methods and attributes, you can use static import for static classes. For example, the class java.awt.geom.Point2D.Double
is a static subclass of java.awt.geom.Point2D
. If you import java.awt.geom.Point2D.Double
, it can refer to all of its static members by using Double.xxx
. The following example, StaticClassEx
, uses the static field out
in the final class, java.lang.System
, to simplify calls to System.out.println()
. StaticClassEx
prints to standard out using out.println()
, and calls the static method distance()
using Double.distance()
.
除了静态方法和属性,您可以使用静态导入来引用静态类。例如,java.awt.geom.Point2D.Double
是
java.awt.geom.Point2D
的一个子类。如果您导入
java.awt.geom.Point2D.Double
,就可以像使用
Double.xxx
一样引用它的静态成员。下面的例子,
StaticClassEx
,使用类
java.lang.System
中的
out
,为了简化对
System.out.println()
的调用。
StaticClassEx
使用
out.println()
来打印标准化输出,使用
Double.distance()
来调用静态方法
distance()
。
package staticEx;
import static java.awt.geom.Point2D.Double;
import static java.lang.System.out;
public class StaticClassEx {
public static void main(String[] args) {
out.println("The diagonal of a unit square is " +
Double.distance(0.0, 0.0, 1.0, 1.0) + ".");
}
}
The result is:
结果:
The diagonal of a unit square is 1.4142135623730951.
When used judiciously, static imports can make your code simpler to read and make it easier to work with static variables, methods, and classes. Be careful not to overuse this technique. Also be sure to explicitly import a variable or method when there is a possibility of ambiguity.
For additional information about static imports, see the section Static Import in the description of new features and enhancements for J2SE 5.0.
如果使用得当,静态导入可以使您的代码更加容易阅读并可以很好的与静态变量,方法和类协作。但不要过度使用这个技术。当有可能产生代码模糊时使用显示的导入变量和方法也是有必要的。关于静态导入的更多信息请参阅J2SE5.0新特性与增强功能的静态导入部分。 (ttp://java.sun.com/j2se/<chsdate w:st="on" year="1899" month="12" day="30" islunardate="False" isrocdate="False">1.5.0</chsdate>/docs/guide/language/static-import.html)
分享到:
相关推荐
babel插件将动态导入转换为静态导入将动态导入转换为静态导入的转换安装: npm install babel-plugin-transform-dynamic-imports-to-static-imports用法:带有配置文件: { " plugins " : [ " babel-plugin-...
其中,导入static元素是Java编程中一个重要的操作,它允许我们直接引用类中的静态成员,无需创建类实例。本篇文章将深入探讨如何在Eclipse中有效地导入static元素,并分享一些实用的开发技巧。 首先,我们需要理解...
Imports Fixer v1.6 是一个专门用于修复dump文件导入表的工具,它在IT行业中主要用于处理二进制文件(如可执行程序)的问题。导入表是PE(Portable Executable)格式文件(如Windows下的exe和dll)的重要组成部分,...
Additionally, it covers static imports and annotations, which are used for code simplification and metadata specification, respectively. ### Conclusion "Java - A Beginner’s Guide - Sixth Edition" ...
静态导入staticimports提供实用程序功能,R程序员可以在其程序包或其他项目中使用它们。 “静态”部分意味着功能是静态“导入”的-即它们以文本形式复制到项目中,而不是在运行时从程序包中加载。 这样做的好处: ...
然而,无论哪种方式,如果导入的文件在实际代码中并未被使用,它们就变成了所谓的“未使用导入”。 为了找出并移除这些未使用的导入,开发者可以采取以下几种方法: 1. **Xcode的Infer**:Xcode提供了一个名为...
现在,您需要重构Ansible代码,创建分配并学习如何使用导入功能。 导入允许在新的剧本中有效地重用以前创建的剧本-它允许您组织任务并在需要时重用它们。 是计算机编程中的通用术语。 这意味着在不更改软件预期行为...
eclipse 的code style 下的cleanup 格式化模板 ...Sort members excluding fields, enum constants, and initializers Organize imports Format source code Correct indentation Remove redundant type arguments
静态导入 (Static Imports) 静态导入允许直接使用静态成员,而无需指定类名,从而进一步简化了代码。这在使用频繁的静态方法或常量时特别有用。 例如,可以使用静态导入来简化 `Math` 类的使用: ```java import...
- **静态导入(Static Imports)**: 通过静态导入可以简化代码,减少重复书写静态成员名称的次数。 - **可变长参数(Varargs)**: 方法可以接受不定数量的参数,这些参数会被当作数组来处理。 - **自动装箱和拆箱...
这与在VS Code中使用“组织导入”操作相同。安装npm install --save-dev prettier-plugin-organize-imports prettier和typescript是对等依赖项,因此请确保已在项目中安装了它们。用法该插件将由Prettier自动加载。...
本文将深入探讨`smart_imports-0.2.7`这个版本,阐述其功能、使用方法以及如何提升Python代码的可维护性和效率。 `smart_imports`是一个Python库,它的主要目的是自动化和优化Python代码中的导入语句。通过智能分析...
before taking a quick tour through the latest features of C# 6 such as string interpolation for easier variable value output, exception filtering, and how to perform static class imports. We'll also ...
与isort类似,但更多地使用静态分析。 安装 pip install reorder-python-imports 控制台脚本 有关完整的选项集,请参阅reorder-python-imports --help 。 reorder-python-imports将文件名作为位置参数 常用选项:...
在Kotlin中,数值常量可以通过使用下划线来提高可读性,如`1_000_000`。同时,类型转换可以通过`toXxx`方法实现,例如`intValue.toLong()`将`Int`转换为`Long`。 在比较方面,Kotlin区分了`==`和`===`。`==`用于值...
排序导入 自动对JavaScript和TypeScript的ES6导入进行排序。 由从软件包移植而来。 特征 保存时自动对导入进行排序。 您可以在设置中禁用此行为并手动对导入进行排序: 启动命令面板( Ctrl/Cmd + Shift + P ); ...
- 可以使用 `using static` 指令来导入一个类的所有静态成员,从而简化调用。 - 示例代码: ```csharp using static System.Math; double result = Max(10, 20); ``` 通过以上介绍,可以看出C# 3.0为.NET...
#### 六、静态导入(Static Imports) 静态导入允许开发者在不使用类名前缀的情况下直接访问静态成员,这使得代码更加简洁。例如,使用`Math`类中的`sin`方法,如果使用静态导入,则可以省略`Math.`前缀: ```java...
babel-plugin-no-index-imports 此插件可以将索引导入转换为直接文件导入。 例如,该插件可以转换导入,例如 import { Homepage } from '@components' ; 定位到默认样式或命名的导入,以特定文件为目标。 import...