2024-04-20 09:00:01

Java的隐藏特性

在阅读了c#的隐藏特性之后,我想知道Java的隐藏特性有哪些?


当前回答

在Swing中工作时,我喜欢隐藏的Ctrl - Shift - F1特性。

它转储当前窗口的组件树。 (假设您没有将该击键绑定到其他内容。)

其他回答

我喜欢方法的静态导入。

例如,创建以下util类:

package package.name;

public class util {

     private static void doStuff1(){
        //the end
     }

     private static String doStuff2(){
        return "the end";
     }

}

然后像这样使用它。

import static package.name.util.*;

public class main{

     public static void main(String[] args){
          doStuff1(); // wee no more typing util.doStuff1()
          System.out.print(doStuff2()); // or util.doStuff2()
     }

}

静态导入适用于任何类,甚至数学…

import static java.lang.Math.*;
import static java.lang.System.out;
public class HelloWorld {
    public static void main(String[] args) {
        out.println("Hello World!");
        out.println("Considering a circle with a diameter of 5 cm, it has:");
        out.println("A circumference of " + (PI * 5) + "cm");
        out.println("And an area of " + (PI * pow(5,2)) + "sq. cm");
    }
}

使用静态导入,你可以做一些很酷的事情,比如:

List<String> myList = list("foo", "bar");
Set<String> mySet = set("foo", "bar");
Map<String, String> myMap = map(v("foo", "2"), v("bar", "3"));

我投票给java.util.concurrent,因为它的并发集合和灵活的执行器允许线程池、计划任务和协调任务。DelayQueue是我个人最喜欢的,其中元素在指定的延迟后才可用。

timer和TimerTask可以安全地休息了。

而且,它不是完全隐藏的,而是在与日期和时间相关的其他类不同的包中。timeunit在纳秒、微秒、毫秒和秒之间转换时很有用。

它比通常的someValue * 1000或someValue / 1000读起来要好得多。

您可以重写一个方法,并让超类构造函数调用它(这可能会让c++程序员感到惊讶)。

例子

Java 6以来的类路径通配符。

java -classpath ./lib/* so.Main

而不是

java -classpath ./lib/log4j.jar:./lib/commons-codec.jar:./lib/commons-httpclient.jar:./lib/commons-collections.jar:./lib/myApp.jar so.Main

参见http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html