import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
class person{
// default (friendly) 在同一个类或同一个包内
// private 同一个类
// protected 同类、同包、不同包子类
// public 同类、同包、不同包子类、不同包非子类 即所有
String name;
int age;
String sex;
public person(String name,int age,String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public void action() {
System.out.println("");
}
}
class teacher extends person{
String job;
public teacher(String name,int age,String sex,String job){
// 继承父类的的属性
super(name,age,sex);
this.job = job;
}
public void stand(){
System.out.println("老师站着");
}
// 重写、覆盖 override
public void action() {
System.out.println("教书育人");
}
}
class student extends person{
public student(String name,int age,String sex) {
super(name,age,sex);
}
public void seat(){
System.out.println("学生坐着");
}
// 重写、覆盖 override
public void action() {
System.out.println("好好上课");
}
}
public class test {
// 向上转型
public static void job(person P) {
P.action();
}
public static void main(String []args) {
// 不转型就要写若干个 public static void job(type t) 函数
// job(new type1) job(new type2) .......
// 向上转型
// job(new type) type 任何子类
// python sorted 函数的方法很可能就是类似原理
job(new teacher("jack",30,"男","老师"));
job(new student("smith",21,"男"));
List persons = new ArrayList();
persons.add(new teacher("tom",25,"男","老师"));
persons.add(new student("D",21,"女"));
// 向下转型
teacher Teacher = (teacher)persons.get(0);
Teacher.stand();
student Student = (student)persons.get(1);
Student.seat();
// 向下转型最大的作用是Java的泛型编程 // 保存子类的信息
}
}