java问题求年龄

来源:百度知道 编辑:UC知道 时间:2024/06/05 21:36:32
定义一个datacount类,起成员变量有year,month,day;创建datacount对象并通过构造方法初始化成员变量.输出datacount类对象值.假定成员变量的值代表人的出生日期.计算当前日期人的年龄.获取系统日期作为当前日期

import java.util.Calendar;

public class DataCount {

int year;
int month;
int day;

public DataCount(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}

public void tellBirthDay() {
System.out.println("生日:" + year + "年" + month + "月" + day + "日");
}

public void tellAge() {
Calendar ca = Calendar.getInstance();
int thisYear = ca.get(Calendar.YEAR);
System.out.println("年龄:" + (thisYear - this.year));
}

public static void main(String[] args) {
DataCount dt = new DataCount(1985, 6, 26);
dt.tellBirthDay();
dt.tellAge();
}
}

楼上的不错
up~~~~~~~~~~~~`