Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请教:如何才能保持一个类只有一个实例对像呀?
naughty009





发贴: 56
积分: -2
于 2005-07-07 17:39 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
请教:如何才能保持一个类只有一个实例对像呀?高手帮帮忙呀~!


作者 Re:请教:如何才能保持一个类只有一个实例对像呀? [Re:naughty009]
ww1ww1





发贴: 202
积分: 5
于 2005-07-07 19:52 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
感觉是这样:
constructor 用static 来修饰


class OnlyOne {
static OnlyOne() { }
}


Is that right?



作者 Re:请教:如何才能保持一个类只有一个实例对像呀? [Re:naughty009]
naughty009





发贴: 56
积分: -2
于 2005-07-07 20:59 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
哈~!那就奇了
好像还可以这样吧
OnlyOne o1=new OnlyOne()
OnlyOne o2=new OnlyOne()
OnlyOne o3=new OnlyOne()

还是可以实例N个对像吧~


naughty009 edited on 2005-07-07 21:59

作者 Re:请教:如何才能保持一个类只有一个实例对像呀? [Re:naughty009]
ww1ww1





发贴: 202
积分: 5
于 2005-07-07 21:08 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

OnlyOne o1=new OnlyOne
OnlyOne o2=new OnlyOne
OnlyOne o3=new OnlyOne


这个我怎么看上去就奇。
自己随便找本书看看,不要是盗版的(可能有印刷错误)。这样的东西也写得出来。。。faint


ww1ww1 edited on 2005-07-07 21:10

作者 Re:请教:如何才能保持一个类只有一个实例对象呀? [Re:naughty009]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2005-07-07 21:12 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

search for Singleton in this forum (Design Patterns board)



作者 Re:请教:如何才能保持一个类只有一个实例对像呀? [Re:naughty009]
ww1ww1





发贴: 202
积分: 5
于 2005-07-07 22:54 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
还挺复杂的,看来要继续好好学习啦


作者 Re:请教:如何才能保持一个类只有一个实例对像呀? [Re:naughty009]
ranchgirl



版主


发贴: 801
积分: 132
于 2005-07-08 06:37 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Give you an example, such as President, one country should have one, I guess. Now, you use singleton pattern is justified.


// Only one President in one country
// Singleton usage is justified
public class President {
private static President currPresident = null;

private President() {
// Exists only to defeat instantiation.
}
public static President getInstance() {
if (currPresident == null) {
currPresident = new President();
}
return currPresident;
}
}


When you need a President, you call President.getInstance(). If we have a President, the current President is returned, otherwise, create (vote) a new President. Smile

Now, your purpose "一个类只有一个实例对像" President is reached. Big Smile


gongshi edited on 2005-07-08 06:44

作者 Re:请教:如何才能保持一个类只有一个实例对像呀? [Re:naughty009]
naughty009





发贴: 56
积分: -2
于 2005-07-08 10:17 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
哈~!谢谢楼上~~~~


作者 Re:请教:如何才能保持一个类只有一个实例对像呀? [Re:naughty009]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2005-07-08 10:43 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
naughty009 wrote:
哈~!谢谢楼上~~~~

sigh...Sad
you really should have searched on this forum before you posted this question.



作者 Re:请教:如何才能保持一个类只有一个实例对像呀? [Re:naughty009]
naughty009





发贴: 56
积分: -2
于 2005-07-08 16:56 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
I have found everywhere on this forum,but none of this question.
thanks a lot.
Big Smile



作者 Re:请教:如何才能保持一个类只有一个实例对像呀? [Re:naughty009]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2005-07-08 23:25 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
naughty009 wrote:
I have found everywhere on this forum,but none of this question.
thanks a lot.
Big Smile

Everywhere?!?!
Lair!
Bullshit! Sad
search with keyword : singleton
Singleton Pattern 的java实现(代码)
http://www.cjsdn.net/post/view?bid=17&id=43923



作者 Re:请教:如何才能保持一个类只有一个实例对像呀? [Re:why]
ranchgirl



版主


发贴: 801
积分: 132
于 2005-07-08 23:33 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
why wrote:
Everywhere?!?!
Lair!
Bullshit! Sad
search with keyword : singleton
Singleton Pattern 的java实现(代码)
http://www.cjsdn.net/post/view?bid=17&id=43923


WHY, you have a bad mode today, I guess. Smile

I think you have been unreasonable with naughty009.

What if he had never heard the word "singleton", how did he know to search for "singleton"?

Put yourself in his/her shoes might help...


gongshi edited on 2005-07-08 23:35

作者 Re:请教:如何才能保持一个类只有一个实例对像呀? [Re:gongshi]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2005-07-09 00:21 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
gongshi wrote:
I think you have been unreasonable with naughty009.

What if he had never heard the word "singleton", how did he know to search for "singleton"?

Put yourself in his/her shoes might help...

Please take a look at my post on this topic, I wrote:
search for Singleton in this forum (Design Patterns board)


Yes, I'm in a bad mood, my back has been hurting me duing the past few days. But I'm mad because I am very disappointed with the newbies in general.


why edited on 2005-07-09 00:28

作者 Re:请教:如何才能保持一个类只有一个实例对像呀? [Re:naughty009]
naughty009





发贴: 56
积分: -2
于 2005-07-09 09:56 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
It doesn't matter , please don't responsible for why
You are my god.
SuN034



作者 Re:请教:如何才能保持一个类只有一个实例对像呀? [Re:naughty009]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2005-07-09 11:12 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
naughty009 wrote:
It doesn't matter , please don't responsible for why
You are my god.

DeadDeadDead
Did you read my post (the fifth one of this topic) when you wrote:
I have found everywhere on this forum,but none of this question.

What had you searched? EVERYWHERE?!?!?!



作者 Re:请教:如何才能保持一个类只有一个实例对像呀? [Re:naughty009]
johnyq





发贴: 8
积分: 0
于 2005-07-09 14:06 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这个是单子的模型
用到其实就是 static private 几个关键字的使用




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923