class Person {
// Person(){
// System.out.println("*******");
// }
public void fun1() {
System.out.println("1 Person(fun1)");
}
public void fun2() {
System.out.println("2 Person(fun2)");
}
public void fun3(int a,int b) {
System.out.println(a+b);
// return a+b;
}
}
class Student extends Person{
public void fun1() {
System.out.println("1 Student(fun1)");
}
public void fun2() {
System.out.println("2 Student(fun2)");
}
public void fun3() {
System.out.println("3 Student(fun3)");
}
public void fun4() {
System.out.println("4 Student(fun4)");
}
}
class StudentExtendsPersonTest{
public static void main(String []args) {
// Person p = new Person();
// p.fun1();
// p.fun2();
// p.fun3(3,5);
// Student s = new Student();
// s.fun1();
// s.fun2();
// s.fun3();
// s.fun4();
Person p = new Student();//向上转型
// p2.fun1();
// p2.fun2();//转型之后,p2会先调用子类已经覆写的父类的方法;如没有覆写,则还是调用p2自己的方法
// p2.fun3(3, 6);
Student s2 = (Student)p;//向下转型
// s2.fun1();
// s2.fun2();
// s2.fun3();
// s2.fun4();
p.fun1();
p.fun2();
p.fun3(5, 8);
}
} |