java创建一个 `Print`接口,它有方法print();创建一个 `Person接口` ,它有方法setData()和getData()对属性name, sex, birthday赋值和获得这些属性组成的字符串信息;创建类 `Student` 实现 `Person`接口和`Print`接口 ,并重写setData()成员方法,设置学生属性的成员变量sID, speciality值,重写getData()获得学生成员变量值所组成的字符信息,以及重写print()方法,从this.getData()方法获取info后打印出来。
时间: 2024-02-27 17:54:33 浏览: 25
好的,以下是你所需的代码:
```java
interface Print {
void print();
}
interface Person {
void setData(String name, String sex, String birthday);
String getData();
}
class Student implements Person, Print {
String name;
String sex;
String birthday;
String sID;
String speciality;
@Override
public void setData(String name, String sex, String birthday) {
this.name = name;
this.sex = sex;
this.birthday = birthday;
}
@Override
public String getData() {
return "Name: " + this.name + ", Sex: " + this.sex + ", Birthday: " + this.birthday + ", SID: " + this.sID + ", Speciality: " + this.speciality;
}
@Override
public void print() {
String info = this.getData();
System.out.println(info);
}
}
```
在这个例子中,我们定义了两个接口:`Print`和`Person`。`Print`接口有一个`print()`方法,`Person`接口有`setData()`和`getData()`方法,用于设置和获取人员信息。
然后我们创建了一个`Student`类,它实现了`Person`和`Print`接口。我们重写了`setData()`、`getData()`和`print()`方法,以便我们可以设置和获取学生的属性,并打印学生信息。
你可以这样使用这个类:
```java
public static void main(String[] args) {
Student student = new Student();
student.setData("张三", "男", "1999-01-01");
student.sID = "1001";
student.speciality = "计算机科学";
student.print();
}
```
运行这段代码后,你应该会看到以下输出:
```
Name: 张三, Sex: 男, Birthday: 1999-01-01, SID: 1001, Speciality: 计算机科学
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)