Topic: 请问类定义中能不能 new自己

  Print this page

1.请问类定义中能不能 new自己 Copy to clipboard
Posted by: wxjohn
Posted on: 2005-08-05 16:58

public class A {
public A {

}

public methord1(){
A a=new A();
}

}

2.Re:请问类定义中能不能 new自己 [Re: wxjohn] Copy to clipboard
Posted by: fuliang2008
Posted on: 2005-08-06 22:15

如果变量a是static就可以。
在设计模式中的单件模式就是如此。程序如下
class Singleton
{
private static Singleton instance = null;
private Singleton(){}
public Singleton getInstance()
{
if(instance == null)
instance = new Singleton();
return instance;
}
}

3.Re:请问类定义中能不能 new自己 [Re: fuliang2008] Copy to clipboard
Posted by: heaven
Posted on: 2005-08-07 00:02

fuliang2008 wrote:
如果变量a是static就可以。

不需要这个约束。

4.Re:请问类定义中能不能 new自己 [Re: wxjohn] Copy to clipboard
Posted by: ranchgirl
Posted on: 2005-08-08 03:22

wxjohn wrote:
请问类定义中能不能 new自己


Yes, why not???

However, I've a question for you. Why don't you just compile and run your own code by yourself? You probably would get your own answer in 3 minutes.

Another advantage of do-it-yourself is that you don't need to wait for somebody's misleading answers such as this one:

fuliang2008 wrote:
如果变量a是static就可以。


There are some more misleading stuff here:
http://www.cjsdn.net/post/view?bid=53&id=151547&sty=1&tpg=1&age=30

5.Re:请问类定义中能不能 new自己 [Re: wxjohn] Copy to clipboard
Posted by: JavaandC
Posted on: 2005-08-25 13:35

当然可以。不过你程序写的有问题
应该这样写:
public class A
{
public A (){}

public void methord1()
{
A a=new A();
}

}


   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