50分悬赏 懂JAVA的进

来源:百度知道 编辑:UC知道 时间:2024/06/18 18:08:30
编写类Person,描述人的信息,类的成员变量有:名字name、性别sex、年龄age。Person类还要求有一个表达出生年月日的属性birth,用什么类型表达,用String勉强可以,但不太好。能不能设计一个新的类型Date来描述日期呢,Date类里面的属性包括year、month、day,这三个属性都可以用String表达。试试用你设计的Date来描述Person里面的birth属性,修改Person的构造函数,修改main代码来验证你的想法。

注意:这种在Person类里面使用Date类不是继承关系,而是组合关系。请好好体会。

大家帮帮忙 马上要考试了 就考这道 我不会 谁能帮忙写写 小弟在此谢过了

/*
Date用的是int型,而且输入年份的时候得4位数。因为时间短就没做数据校验了
*/
public class Date {
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 Date(){

}
public Date(int y,int m,int d){
year=y;
month=m;
day=d;
}
public String toString(){
String birth=year+"-"+month+"-"+day;
return birth;
}
}

public class Person {
private String name;
private String gender; //我喜欢用string来表示性别。。直接
private Date birthday;

public Person(){}
public Person(String n,