- package 多态;
- abstract class Student
- {
- public abstract void study();
- public void sleep()
- {
- System.out.println("Sleep Sleep");
- }
- }
- class BaseStudent extends Student
- {
- public void study() {}
- public void sleep() //对父类函数的重写
- {
- System.out.println("Sleep-Sleep");
- }
- }
- class AdvStudent extends Student
- {
- public void study()
- {
- System.out.println("Adv student");
- }
- //public void sleep(){}
- }
- class DoStudent //将重复要做的动作单独抽取出来
- {
- public void function(Student stu)
- {
- stu.study();
- stu.sleep();
- }
- }
- public class DuotaiDemo
- {
- public static void main(String[] args)
- {
- DoStudent ds=new DoStudent();
- ds.function(new BaseStudent());
- ds.function(new AdvStudent());
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/3107201410063.html
来源: http://www.codesnippet.cn/detail/3107201410063.html