关于this调用构造方法为什么不行呢?

renqiang_888 2007-04-12 03:33:18
public class aa
{
aa()
{
System.out.println("我是AA的构造方法");
}
void qq()
{ this();

}
public static void main(String args[])
{
aa t=new aa();
t.qq();
}
}

提示:"call to this must be first statement in constructor"
但是此时this();已经放在statement的第一句了,请问该如何解决这个问题?
...全文
334 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
cangying747 2007-04-12
  • 打赏
  • 举报
回复
lz很有创意,不过也不是完全不可能,可以将类写成singleton,然后函数里调用new this()。应该和lz的想法比较接近了。
KRplusSRequalGOD 2007-04-12
  • 打赏
  • 举报
回复
真逗
luoxiaobo227 2007-04-12
  • 打赏
  • 举报
回复
搞了半天还是懂啦 谢谢啦
lanseliuying 2007-04-12
  • 打赏
  • 举报
回复
构造方法是创建对象的时候自动调用的,不用你自己调用。
家有萌宝V 2007-04-12
  • 打赏
  • 举报
回复
rypgood(失魂) ( ) 信誉:100 Blog 加为好友 2007-04-12 17:09:47 得分: 0


怎么可以在普通方法里调用构造方法呢
————————————————————————
什么意思

上面在main方法调用构造函数创建对象,就是例子啊


rypgood 2007-04-12
  • 打赏
  • 举报
回复
怎么可以在普通方法里调用构造方法呢
renqiang_888 2007-04-12
  • 打赏
  • 举报
回复
谢谢终于明白了,我这样写比较容易理解,希望有THIS困惑的朋友看了之后可以理解如下:
public class aa
{
aa()
{ this("a");
System.out.println("我是AA的构造方法");

}
aa(String temp)
{ this("a","b");
System.out.println("我是第二个构造函数");
}
aa(String a,String b)
{
System.out.println("我是第三个构造函数");
}
public static void main(String args[])
{
aa t=new aa();
}
}
momo8303 2007-04-12
  • 打赏
  • 举报
回复
this()只能用在构造函数中,调用其他的构造函数
zhuokai 2007-04-12
  • 打赏
  • 举报
回复
this();请用在Constructor(构造函数)中:
aa(String str)
{
System.out.println(str);
}
aa()
{
this("test");
}
zhaochunhui 2007-04-12
  • 打赏
  • 举报
回复
构造器只能在构造器中调用~!
如:
public class Hello{
public String s;
public Hello(){
this("aaaa");
}
public Hello(String args){
this.s=args
}
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧