java--构造方法

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

 class haha{
    public haha(){
        System.out.println("Hello Constructor");
        }
    public haha(String hehe){
        this();
        System.out.println(hehe);
}
 }
public class test{
   public static void main(String[]args){
    //匿名对象 new haha(args[0]);
    haha s=new haha("abc"); //实例化
   } 
}

猜你喜欢

转载自blog.csdn.net/weixin_42365095/article/details/83047595