J2SE5.0 Static Import of New Features

xiaoxiao2021-03-06  51

J2SE5.0 Static Import of New Features

晁 晁 攀 Smallnest@163.com

Before J2SE5.0, if you need to use other classes, such as the method in java.lang.math, you need to write the following code:

Double value = math.log (100) * math.pi;

Now you only need to import static, then you can use static methods and static fields directly in your code:

Import static java.lang.math. *;

...

Double value = log (100) * pi;

Look at a detailed example. We first build a class containing a static method and a static field:

Package com.kuaff.jdk5; public class staticclass {public static string label = "Ancient Chinese Sword"; public static void printlist () {string [] swords = new string [] {"Xuanyuan Xia Sword", "Zhan", "Chiao", "Tai A", "Long Yuan", "Dry will", "Mo Xie", "Fish Intestines", "Pure", "In (String Name: Swords) {System.out .printf ("% s% n", name);}}}

Newly built a class, static imported the fields and methods, and use them:

Package com.kuaff.jdk5; import static com.kuaff.jdk5.staticclass. *; // Static Import Public Class StaticimportShow {PUBLIC VOID TESTSI () {System.out.Printf ("% s:% n", label); PRINTLIST (); public static void main (string [] args) {staticimportshow show = new staticimportshow (); show.testsi ();}}

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

New Post(0)