创建一个带默认构造方法(即无参构造)的类,在构造方法中打印一条消息,再为这个类添加一个重载构造方法,令其接收一个字符串参数,并在这个有参构造方法中把前一条消息和接收的参数一起打印出来。
class Person{
private String name="宋典";private int age=21;
public Person(){
System.out.println(name+age);
}
public Person(String name,int age){
this();//构造方法的调用,不要带方法名不带点(.)直接this();
System.out.println("有参构造"+name+","+age);
}
}
class Personal{
private String name;
private int num;
public void setName(String name){
this.name=name;
}
public String getName(String name){
return name;
}
public void setNum(int num){
this.num=num;
}
public int getNum(int num){
return num;
}
public void getinformation(){
System.out.println(name+","+num);
}
}
class Day2{
public static void main(String[] args){
Person per=new Person("有形参数",2);
Person per2=new Person();
Personal per3=new Personal();
per3.setNum(12);
per3.setName("私有属性");
per3.getinformation();
}
}