作业1:声明一个日期类MyDate,包含属性:年、月、日 声明一个Test02测试类,并在main方法中,创建3个日期对象,一个是你的出生日期,一个是随机定义的日期,一个是今年过年的日期,并打印显示,用JAVA实现完整代码
时间: 2023-05-27 22:03:15 浏览: 187
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 String toString() {
return year + "年" + month + "月" + day + "日";
}
}
```
Test02测试类代码
```java
import java.util.Random;
public class Test02 {
public static void main(String[] args) {
MyDate myBirthDate = new MyDate(1995, 10, 7);
MyDate randomDate = new MyDate(2022, 7, 16);
MyDate newYearDate = new MyDate(2023, 1, 1);
System.out.println("我的出生日期是:" + myBirthDate.toString());
System.out.println("随机日期是:" + randomDate.toString());
System.out.println("今年过年是:" + newYearDate.toString());
}
}
```
阅读全文