- /*
- 基础班学生:学习,睡觉
- 高级版学生:学习,睡觉
- */
- abstract class Student
- {
- public abstract void study();
- public void sleep()
- {
- System.out.println("躺着睡");
- }
- }
- class BaseStudent extends Student
- {
- public void study()
- {
- System.out.println("基础班学生学习");
- }
- public void sleep()
- {
- System.out.println("坐着睡");
- }
- }
- class AdvStudent extends Student
- {
- public void study()
- {
- System.out.println("高级版学生学习");
- }
- }
- class DoSome
- {
- public void doSome(Student s)
- {
- s.study();
- s.sleep();
- }
- }
- class Demo4
- {
- public static void main(String[] args)
- {
- /*BaseStudent b=new BaseStudent();
- b.study();
- b.sleep();
- AdvStudent a=new AdvStudent();
- a.study();
- a.sleep();
- */
- //doSome(new BaseStudent());
- DoSome d=new DoSome();
- d.doSome(new BaseStudent());
- }
- public static void doSome(Student s)
- {
- s.study();
- if(s instanceof BaseStudent)
- {
- BaseStudent base=(BaseStudent)s;
- base.sleep();
- }
- }
- }
复制代码
|
|