SHAOXIAOJ正在加载中...

2271: 编程题-多态-上课

金币值:2 定数:6 时间限制:1.000 s 内存限制:128 M
正确:0 提交:0 正确率:0.00% 命题人:
点赞量:0 收藏量:0 题目类型:程序 知识点: Java

题目描述

使用Java多态思想编程实现"上课”功能,编写教师、学生上课多态程序。
(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行:
教师姓名
学生姓名

输出格式

见样例

输入样例    复制

李琼
王琴

输出样例    复制

李琼在授课
王琴在听课