JDK1.5新特性(二)……Static Import

援引

Static Import - This facility lets you avoid qualifying static members with class names without the shortcomings of the "Constant Interface antipattern.

用法

import static java.util.Arrays.*;

导入的是Arrays这个类中的所有静态成员

当类名重名时,需要指定具体的包名

当方法重名时,需要指定具体的对象或类

例子

   1: //import static java.lang.Math.max;//静态导入max静态方法
   2: import static java.lang.Math.*;//静态导入Math类中的所有静态方法
   3:  
   4: /**
   5:  * @author Shawn
   6:  *
   7:  */
   8: public class StaticImport {
   9:  
  10:     public static void main(String[] args) {
  11:         // TODO Auto-generated method stub
  12:         double a = max(3, 6);
  13:         //当静态方法重名时,方法前面要加上类名以示区分
  14:         double b = StaticImport.pow(3, 2);        
  15:         double c = Math.pow(3, 2);
  16:     }
  17:     
  18:     public static double pow(double a,double b){
  19:         return a*b;
  20:     }
  21: }
原文地址:https://www.cnblogs.com/ShawnWithSmallEyes/p/3453936.html