使用静态类!不用生成实例直接用类调用方法!

解决方案 »

  1.   

    使用单例模式。具体的方法就是把构造方法变成私有的。 然后在类中加一个静态的属性保存自已的一个实例。并提供一个得到该实例的方法。通常为 getInstance(); 具体的写法可以这样:public class TestSingeton {
        private static TestSingeton ourInstance;    public synchronized static TestSingeton getInstance() {
            if (ourInstance == null) {
                ourInstance = new TestSingeton();
            }
            return ourInstance;
        }    private TestSingeton() {
        }
    }