里氏代换原则
——子类能替换父类
1 package lishidaihuan; 2 3 class TangSeng { 4 public void QuJing() { 5 System.out.println("施主,贫僧自东土大唐而来,去往西天拜佛求经!"); 6 } 7 } 8 9 class TuDi extends TangSeng { 10 public void QuJing() { 11 super.QuJing(); 12 System.out.println("降妖除魔!"); 13 } 14 } 15 16 public class LiShiDaiHuan { 17 18 public static void main(String[] args) { 19 // TODO Auto-generated method stub 20 21 TangSeng tangSeng = new TangSeng(); 22 System.out.println("唐僧:"); 23 tangSeng.QuJing(); 24 25 tangSeng = new TuDi(); 26 System.out.println("徒弟:"); 27 tangSeng.QuJing(); 28 } 29 }
运行结果:
唐僧:
施主,贫僧自东土大唐而来,去往西天拜佛求经!
徒弟:
施主,贫僧自东土大唐而来,去往西天拜佛求经!
降妖除魔!