本帖最后由 李东城 于 2013-2-6 02:55 编辑
- public class Singleton {
- String name;
- private static Singleton sinleton=null;
- private Singleton(){
- }
- public synchronized static Singleton getSinleton() {
- if(sinleton==null){
- sinleton=new Singleton();
- }
- return sinleton;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- class run{
- public static void main(String[] args) {
- Singleton s1 =Singleton.getSinleton();
- s1.setName("s1");
- Singleton s2 =Singleton.getSinleton();
- s2.setName("s2");
- System.out.println(s1.getName());
- System.out.println(s2.getName());
- }
- }
复制代码 这样子的话就无法运行了、为什么哦? 内部内有这个规定么? |
|