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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值