日期:2014-05-20  浏览次数:20666 次

为什么不带参数的构造函数不能用 Test(3,3); 这种形式调用带参数构造函数 ?
class   Test
{
int   x,y;
Test()
{
//this(3,3);
Test(3,3);                 //为什么不能这么用?
}
Test(int   x,int   y)
{
this.x=x;
this.y=y;
}
public   static   void   main(String   args[])
{
Test   tmp;
tmp=new   Test();
tmp.show();
}
void   show()
{
System.out.println(x);        
System.out.println(y);
}
}

------解决方案--------------------
你应该写作this(3,3);

Test(3,3);是在构造函数之外才这么写的,当然前面还得有个new关键词,这两者要一起出现,表示生成一个新对象。

而在构造函数里,不是生成新对象,而是使用另外的构造函数体,要写成this()。