这个是可以的。
例如:
public class A
{
A()
{
System.out.println("This constructor of A has not parameter."):
}
A(int i)
{
this();//这是非常关键的调用语句。
System.out.pfintln(“This constructor of A has parameter.it is”+i);
}
public static void main(String[] args)
{
int n= 8;
A alnstanee=new A(n);
}
该程序的运行结果如下:
This constructor of A has not parameter.
This consturctor of A has parameter,it is 8
从上例中可以看出在类A的带参构造器中在第一行使用了this()语句,其意义即指调用前商
定义的类A的无参构造器。 作者: 郭娇珍 时间: 2013-1-3 00:44
{:soso_e136:}没看明白,表示愚昧,求详解作者: 奋斗的青春 时间: 2013-1-3 00:47 本帖最后由 吴愿涛 于 2013-1-3 01:04 编辑
郭娇珍 发表于 2013-1-3 00:44
没看明白,表示愚昧,求详解
好了,再看一遍 。
public class A
{
A(){
System.out.println("无参构造器");
}
A(int i){
this();//这是非常关键的调用语句,即为该构造方法调用另一个构造方法!
System.out.println("有参构造器,参数值是: "+i);
}
public static void main(String[] args)
{
int n= 8;
A a1=new A(n);
}
}
复制代码
作者: 郭娇珍 时间: 2013-1-3 01:07
吴愿涛 发表于 2013-1-3 00:47
好了,再看一遍 。
class A
{
A()
{
System.out.println("A");
}
A(int i)
{
System.out.println("b");
}
A(int i,String str)
{
this(int i);
System.out.println("c");
}
public static void main(String[] args)
{
int n=8;String str="haha";
A abc =new A(8,"haha");
}
}