你可以非常简单地用一个enum构建一个单例或者工具类。
import java.io.Serializable; public enum SafeSingleton implements Serializable{ INSTANCE; SafeSingleton(){ // } public void singleton(){ System.out.println("hello!!"); } } @Test public void test16() { // Collections2.filter(unfiltered, predicate); SafeSingleton.INSTANCE.singleton(); }
package guava; public enum Operation { PLUS { double eval(double x, double y) { return x + y; } }, MINUS { double eval(double x, double y) { return x - y; } }, TIMES { double eval(double x, double y) { return x * y; } }, DIVIDE { double eval(double x, double y) { return x / y; } }; abstract double eval(double x, double y); } @Test public void test(){ double eval = Operation.DIVIDE.eval(12, 2); System.out.println(eval); }
原文:http://blog.csdn.net/u012516914/article/details/43567309