JDK1.5 new features ---- Static Import

xiaoxiao2021-03-06  74

Let us now take a look at what is more exciting and innovation in JDK5. I want to introduce a relatively practical characteristic: static import! Look directly, it's easy to understand:

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;

}

// Import static variables and methods in the class

Package staticex;

Import static static sitex.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 provides new features that import static classes, methods, and attributes. It is easy to get started. As for applicable places, you will watch itself!

转载请注明原文地址:https://www.9cbs.com/read-108936.html

New Post(0)