使用静态导入可以使被导入类的静态变量和静态方法在当前类直接可见,使用这些静态成员无需再给出他们的类名。
导入类:
package com.learn.comm;
public class Common {
public static final String HELLO = "hello world!";
public static void show()
{
System.out.println("hello world!");
}
}
正常导入:
package com.learn.imports;
import com.learn.comm.Common;
public class Test {
public static void main(String[] args) {
System.out.println(Common.HELLO);
Common.show();
}
}
静态导入:
package com.learn.imports;
import static com.learn.comm.Common.HELLO;
import static com.learn.comm.Common.show;
public class StaticImportTest {
public static void main(String[] args) {
System.out.println(HELLO);
show();
}
}
静态导入的是类的静态成员变量和静态方法
本文介绍了Java中静态导入的使用方法,通过示例对比了普通导入和静态导入的区别,展示了如何利用静态导入简化代码并提高可读性。
152

被折叠的 条评论
为什么被折叠?



