现在的位置: 首页 > 综合 > 正文

USING STATIC IMPORTS FOR CONSTANTS AND METHODS(使用静态导入引用常量与方法)

2014年02月27日 ⁄ 综合 ⁄ 共 8685字 ⁄ 字号 评论关闭

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_TWOSQRT_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时,可以降低疑惑。用静态导入您可以用PIsin()来简化Math.PIMath.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.Doublejava.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/1.5.0/docs/guide/language/static-import.html)

抱歉!评论已关闭.