package test;
/*
* 多态:多态就是行为具有表现多种功能的能力
* 继承多态:通过继承的方式来体现的多态
*/
abstract class Teacher{
String name;
int age;
public abstract void teach();
}
class JavaTeacher extends Teacher{
public void teach() {
System.out.println(name+"在教java课");
}
}
class PHPTeacher extends Teacher{
public void teach() {
System.out.println(name+"在教PHP课");
}
}
class Leader{
/*
* 考核java老师
* Teacher t = new PHPTeacher(); 子类的对象自动转换成父类
*/
public void check(Teacher t) {
//判断父类的实际真身是哪个子类的实例
if(t instanceof JavaTeacher) {//t如果是JavaTeacher的实例的话
System.out.println("Java老师开始讲课");
}
if(t instanceof PHPTeacher) {//t如果是PHPTeacher的实例的话
System.out.println("PHP老师开始讲课");
}
t.teach();
System.out.println("打分");
}
}
public class Demo1{
public static void main(String[] args) {
//创建一个java老师
JavaTeacher jt = new JavaTeacher();
jt.name = "战哥";
PHPTeacher pt = new PHPTeacher();
pt.name = "老肖";
//创建一个领导对象
Leader leader = new Leader();
leader.check(jt);
System.out.println("--------------------------");
leader.check(pt);
}
}