2271: 编程题-多态-上课
金币值:2
定数:6
时间限制:1.000 s
内存限制:128 M
解决:0
提交:0
正确率:0.00% 命题人:
题目描述
使用Java多态思想编程实现"上课”功能,编写教师、学生上课多态程序。
(1)编写一个Person类,包含haveLesson()方法;编写Teacher类和Student类继承自Person类,并重写haveLesson()方法;
(2)编写一个测试类,提供对Teacher类和Student类进行查看的doLesson()方法。
写出多态程序,并写出测试用例。
(1)编写一个Person类,包含haveLesson()方法;编写Teacher类和Student类继承自Person类,并重写haveLesson()方法;
(2)编写一个测试类,提供对Teacher类和Student类进行查看的doLesson()方法。
写出多态程序,并写出测试用例。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String tname,sname;
Scanner in = new Scanner(System.in);
tname = in.nextLine();
sname = in.nextLine();
doLesson (new Teacher(tname));
doLesson (new Student(sname));
}
public static void doLesson(Person e){
e.haveLesson();
}
}
/* 以下为你的代码 */
输入
输入2行:
教师姓名
学生姓名
教师姓名
学生姓名
输出
见样例
样例输入 复制
李琼
王琴
样例输出 复制
李琼在授课
王琴在听课