/*
实现功能: 多态(polymorphic)中成员方法的调用.
编译看父类,运行看子类.(记住即可实现) 继承中override的特性.
打印输出: 我爱你
*/
- class Demo {
- public static void main(String[] args) {
- A a = new B();
- a.show();
- B b = new C();
- b.show();
- }
- }
- class A {
- public void show(){
- show2();
- }
- public void show2(){
- }
- }
- class B extends A {
- /*public void show(){
- show2();
- }*/
- public void show2(){
- System.out.println("我");
- System.out.println("爱");
- }
- }
- class C extends B {
- public void show(){
- super.show(); //show2();
- }
- public void show2(){
- System.out.println("你");
- }
- }
复制代码
|
|