标签:void 属性 单例 定义 turn 设计 new string word
class Singleton{ private static Singleton instance;//内部实例化对象 public static Singleton getInstance(){ if(instance == null){ instance = new Singleton(); } return instance; } private Singleton(){} //构造方法私有化 public void print(){ System.out.println("hellow word"); } } public class Lxd{ public static void main(String[] args) { Singleton s = null; //声明对象 s = Singleton.getInstance(); //直接访问Static属性 s.print(); //使用方法 } }
class Singleton{ private static final Singleton instance = new Singleton();//内部实例化对象 public static Singleton getInstance(){ //调用实例对象的方法 return instance; } private Singleton(){} //构造方法私有化 public void print(){ System.out.print("hello"); } } public class LxdT { public static void main(String[] args) { Singleton s = Singleton.getInstance(); //类名.方法名调用 s.print(); //使用方法 } }
标签:void 属性 单例 定义 turn 设计 new string word
原文地址:http://www.cnblogs.com/xiandong/p/7979232.html