public class Zhidao {
public static void main(String args[]) {
Student s1 = new Student();
Student s2 = new Student("张三",1989,7,7);
Student s3=new Student("张三",new MyDate());
Student s4=new Student("张三",new MyDate(2009,4,1));
s1.print();
s2.print();
s3.print();
s4.print();
}
}
class MyDate {
private int year;
private int month;
private int day;
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public MyDate(){
this.year = 1999;
this.month = 1;
this.day =1;
}
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}
class Student {
private String name;
private MyDate birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public Student() {
this.name = "default";
this.birthday = new MyDate();
}
public Student(String name,int year,int month,int day) {
this.name = name;
this.birthday = new MyDate(year,month,day);
}
public Student(String name, MyDate birthday) {
super();
this.name = name;
this.birthday = birthday;
}
public void print() {
System.out.println("name=" + name + "\nbirthday=" + birthday.getYear() + "."+
birthday.getMonth() + "." + birthday.getDay() );
}
}
结果:
name=default
birthday=1999.1.1
name=张三
birthday=1989.7.7
name=张三
birthday=1999.1.1
name=张三
birthday=2009.4.1