java创建一个老师和学生都可以收取作业的接口类的测试类
时间: 2023-06-18 19:06:40 浏览: 95
好的,以下是一个简单的 Java 接口类及其测试类的实现,供您参考:
```java
// HomeworkReceiver 接口类
public interface HomeworkReceiver {
void receiveHomework(Homework homework);
}
// Student 类实现 HomeworkReceiver 接口
public class Student implements HomeworkReceiver {
private String name;
public Student(String name) {
this.name = name;
}
@Override
public void receiveHomework(Homework homework) {
System.out.println(name + " received homework: " + homework.getDescription());
}
}
// Teacher 类实现 HomeworkReceiver 接口
public class Teacher implements HomeworkReceiver {
private String name;
public Teacher(String name) {
this.name = name;
}
@Override
public void receiveHomework(Homework homework) {
System.out.println(name + " received homework: " + homework.getDescription());
}
}
// Homework 类
public class Homework {
private String description;
public Homework(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
// Test 类
public class TestHomeworkReceiver {
public static void main(String[] args) {
// 创建一个学生和一个老师
Student student = new Student("Tom");
Teacher teacher = new Teacher("Lucy");
// 创建一个作业
Homework homework = new Homework("Java 接口类");
// 学生和老师都可以收取作业
student.receiveHomework(homework);
teacher.receiveHomework(homework);
}
}
```
在上面的代码中,我们定义了一个 `HomeworkReceiver` 接口类,其中有一个 `receiveHomework` 方法,用于接收作业。然后我们创建了一个 `Student` 和一个 `Teacher` 类,它们都实现了 `HomeworkReceiver` 接口,并实现了 `receiveHomework` 方法。最后我们创建了一个 `TestHomeworkReceiver` 测试类,用于测试我们的代码是否正确。在测试类中,我们创建了一个学生和一个老师,并创建了一个作业,然后分别让学生和老师收取作业。
阅读全文