Topic: 请教:如何才能保持一个类只有一个实例对像呀?

  Print this page

1.请教:如何才能保持一个类只有一个实例对像呀? Copy to clipboard
Posted by: naughty009
Posted on: 2005-07-07 17:39

请教:如何才能保持一个类只有一个实例对像呀?高手帮帮忙呀~!

2.Re:请教:如何才能保持一个类只有一个实例对像呀? [Re: naughty009] Copy to clipboard
Posted by: ww1ww1
Posted on: 2005-07-07 19:52

感觉是这样:
constructor 用static 来修饰


class OnlyOne {
static OnlyOne() { }
}


Is that right?

3.Re:请教:如何才能保持一个类只有一个实例对像呀? [Re: naughty009] Copy to clipboard
Posted by: naughty009
Posted on: 2005-07-07 20:59

哈~!那就奇了
好像还可以这样吧
OnlyOne o1=new OnlyOne()
OnlyOne o2=new OnlyOne()
OnlyOne o3=new OnlyOne()

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

4.Re:请教:如何才能保持一个类只有一个实例对像呀? [Re: naughty009] Copy to clipboard
Posted by: ww1ww1
Posted on: 2005-07-07 21:08


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


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

5.Re:请教:如何才能保持一个类只有一个实例对象呀? [Re: naughty009] Copy to clipboard
Posted by: why
Posted on: 2005-07-07 21:12


search for Singleton in this forum (Design Patterns board)

6.Re:请教:如何才能保持一个类只有一个实例对像呀? [Re: naughty009] Copy to clipboard
Posted by: ww1ww1
Posted on: 2005-07-07 22:54

还挺复杂的,看来要继续好好学习啦

7.Re:请教:如何才能保持一个类只有一个实例对像呀? [Re: naughty009] Copy to clipboard
Posted by: ranchgirl
Posted on: 2005-07-08 06:37

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

8.Re:请教:如何才能保持一个类只有一个实例对像呀? [Re: naughty009] Copy to clipboard
Posted by: naughty009
Posted on: 2005-07-08 10:17

哈~!谢谢楼上~~~~

9.Re:请教:如何才能保持一个类只有一个实例对像呀? [Re: naughty009] Copy to clipboard
Posted by: why
Posted on: 2005-07-08 10:43

naughty009 wrote:
哈~!谢谢楼上~~~~

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

10.Re:请教:如何才能保持一个类只有一个实例对像呀? [Re: naughty009] Copy to clipboard
Posted by: naughty009
Posted on: 2005-07-08 16:56

I have found everywhere on this forum,but none of this question.
thanks a lot.
Big Smile

11.Re:请教:如何才能保持一个类只有一个实例对像呀? [Re: naughty009] Copy to clipboard
Posted by: why
Posted on: 2005-07-08 23:25

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

12.Re:请教:如何才能保持一个类只有一个实例对像呀? [Re: why] Copy to clipboard
Posted by: ranchgirl
Posted on: 2005-07-08 23:33

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...

13.Re:请教:如何才能保持一个类只有一个实例对像呀? [Re: gongshi] Copy to clipboard
Posted by: why
Posted on: 2005-07-09 00:21

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.

14.Re:请教:如何才能保持一个类只有一个实例对像呀? [Re: naughty009] Copy to clipboard
Posted by: naughty009
Posted on: 2005-07-09 09:56

It doesn't matter , please don't responsible for why
You are my god.
SuN034

15.Re:请教:如何才能保持一个类只有一个实例对像呀? [Re: naughty009] Copy to clipboard
Posted by: why
Posted on: 2005-07-09 11:12

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?!?!?!

16.Re:请教:如何才能保持一个类只有一个实例对像呀? [Re: naughty009] Copy to clipboard
Posted by: johnyq
Posted on: 2005-07-09 14:06

这个是单子的模型
用到其实就是 static private 几个关键字的使用


   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