多态使用前提:1.对父类进行重写或对接口进行实现 2.向上转型。
abstract class People {
abstract void sleep();
abstract void eat();
}
class Student extends People {
void sleep() {
System.out.println("我睡了");
}
void eat() {
System.out.println("我吃了");
}
}
class Student2 extends People {
void sleep() {
System.out.println("我不睡");
}
void eat() {
System.out.println("我不吃");
}
}
class PeopleRun {
public static void run(People p) {
p.sleep();
p.eat();
}
}
public class Demo {
public static void main(String[] args) {
PeopleRun.run(new Student());
PeopleRun.run(new Student2());
}
}