多态
1package Lesson01; 2 3public class Demo001 { 4 5 public static void main(String[] args) { 6 /* 7 * 超人案例(深入理解多态-隐藏-低调-伪装) 8 * 9 * 超人去美国找某某集团的老总谈生意 超人在别人面前,如果不说自己是超人,在别人面前表现的就是普通人 老总以为是谈小生意的,但实际是谈大生意 10 * 老总以为他不会飞,实际上他会飞去救人 11 */ 12 13 // 父类指向子类对象(多态) 14 Person p = new SupperMan(); 15 p.fly(); 16 SupperMan sm = new SupperMan(); 17 sm.fly(); 18 19 SpiderMan sp1 = new SpiderMan(); 20 sp1.fly(); 21// Person p1 = new Person(); 22// SupperMan sm2 = (SupperMan) p1; 23// sm2.fly(); 24 25 26 test1(sm); 27 test2(sp1); 28 test(sm); 29 test(sp1); 30 } 31 public static void test(Person per) 32 { 33 per.fly(); 34 } 35 36 public static void test1(SupperMan sm) { 37 sm.fly(); 38 } 39 40 public static void test2(SpiderMan sm) { 41 sm.fly(); 42 } 43 44} 45 46 47 // 普通人 48 class Person { 49 public void walk() { 50 System.out.println("走...."); 51 } 52 53 public void fly() { 54 System.out.println("我是普通人,不会飞..."); 55 } 56 } 57 58 // 超人 59 class SupperMan extends Person { 60 public void fly() { 61 System.out.println("超人飞去救人..."); 62 } 63 } 64 65 // 蜘蛛 66 class SpiderMan extends Person{ 67 public void fly(){ 68 System.out.println("蜘蛛侠爬去救人..."); 69 } 70 }