您的位置:首页 > 其它

单例模式

2015-11-05 15:18 225 查看
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26
package
 
test;

 
class
 
Singleton

{

    
private
 
static
 
Singleton instance;

    
private
 
Singleton(){}

    
public
 
static
 
Singleton getInstance()

    
{

        
if
(instance==
null
)

        
{

            
instance=
new
 
Singleton();

        
}

        
return
 
instance;

    
}

}

 
public
 
class
 
JTest

{

    
public
 
static
 
void
 
main(String[] args)

    
{

        
Singleton s1=Singleton.getInstance();

        
Singleton s2=Singleton.getInstance();

        
 
        
System.out.println(s1==s2);

    
}

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: