zoukankan      html  css  js  c++  java
  • JDK5新特性之一静态导入

    现在让我们来看看JDK5中有什么更激动人心的改进和创新,我要介绍的是其中的一个比较实用的特性:静态导入!
    直接看一段代码吧,很容易就可以了解:

    package staticEx;

       public class TranscendentalConstants {

         public static final double PI = 3.14159;

         public static final double E = 2.71828;

       }

    public class IrrationalConstants {

         public static final double SQRT_TWO = 1.414;

         public static final double SQRT_THREE = 1.732;

       }

    //在类中导入静态变量和方法

    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());

         }

       }


    JDK5提供了导入静态类,方法,和属性的新特性,上手也很容易,至于适用的地方嘛,各位就自己看着办罗!

  • 相关阅读:
    2018.4.26 lvm
    2018.4.25 github创建新项目
    2018.4.24 快排查找第K大
    2018.4.24 flask_mail使用
    SpringBoot中使用ES和MongoDB常用API
    设计模式-Template_Method模式
    设计模式-Chain of Responsibility模式
    设计模式-Observer模式
    设计模式-Adapter模式
    设计模式-Strategy模式
  • 原文地址:https://www.cnblogs.com/fengye/p/652385.html
Copyright © 2011-2022 走看看