声明公民类Citizen,包含属性:姓名,生日,身份证号,其中姓名是String类型,生日是MyDate类型,给出MyDate,身份证号也是String类型。 声明Test02测试类,在main方法中创建你们家庭成员的几个对象,并打印信息。
时间: 2024-12-08 13:23:35 浏览: 26
在Java中,你可以这样做:
首先,我们创建一个名为`Citizen`的类,包含姓名、生日和身份证号作为属性:
```java
public class Citizen {
private String name;
private MyDate birthday;
private String idNumber;
// 构造函数
public Citizen(String name, MyDate birthday, String idNumber) {
this.name = name;
this.birthday = birthday;
this.idNumber = idNumber;
}
// 获取和设置方法
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 String getIdNumber() {
return idNumber;
}
public void setIdNumber(String idNumber) {
this.idNumber = idNumber;
}
}
```
接下来,我们需要定义`MyDate`类,这里假设它是一个简单的日期表示类(实际可以更复杂,比如使用`java.time.LocalDate`):
```java
class MyDate {
// 实际实现省略...
// 这里假设MyDate有年月日等属性和相应的getter/setter方法
}
```
然后,我们创建`Test02`测试类并实现main方法:
```java
public class Test02 {
public static void main(String[] args) {
// 创建家庭成员的对象
Citizen father = new Citizen("爸爸", new MyDate(1980, 5, 1), "123456789012345678");
Citizen mother = new Citizen("妈妈", new MyDate(1982, 12, 20), "987654321098765432");
// 打印信息
System.out.println("Father's information:");
System.out.println("Name: " + father.getName());
System.out.println("Birthday: " + father.getBirthday());
System.out.println("ID Number: " + father.getIdNumber());
System.out.println("\nMother's information:");
System.out.println("Name: " + mother.getName());
System.out.println("Birthday: " + mother.getBirthday());
System.out.println("ID Number: " + mother.getIdNumber());
// ... 可能还有其他家庭成员的实例化和打印
}
}
```
阅读全文