创建一个带默认构造方法的类,在构造方法中打印一条消息"Hello Constructor",再为这个类添加一个重载构造方法,令其接收一个字符串参数,并在这个有参构造方法中将他们一起打印出来。

class Print{
public Print(){
System.out.println(“Hello Constructor”);
}
public Print(String s){
this();
System.out.println(“s”);
}

}

在这里插入图片描述
需要注意:
this(参数)表示调用本类的构造方法。
1.this调用本类的构造方法必须写在构造方法首行;
2.this调用构造方法不能成环。

猜你喜欢

转载自blog.csdn.net/qq_44149554/article/details/88192752