用java实现作业1:声明一个日期类MyDate,包含属性:年、月、日 声明一个Test02测试类,并在main方法中,创建3个日期对象,一个是你的出生日期,一个是随机定义的日期,一个是今年过年的日期,并打印显示
时间: 2023-05-27 09:03:18 浏览: 209
// MyDate.java
public class MyDate {
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = 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;
}
@Override
public String toString() {
return year + "-" + month + "-" + day;
}
}
// Test02.java
public class Test02 {
public static void main(String[] args) {
MyDate birthday = new MyDate(1999, 7, 20);
MyDate randomDate = new MyDate(2022, 1, 1);
MyDate newYear = new MyDate(2023, 1, 1);
System.out.println("My birthday is: " + birthday);
System.out.println("A random date is: " + randomDate);
System.out.println("New year is: " + newYear);
}
}
阅读全文