故事贩卖机 2017-12-25
里氏代换原则
——子类能替换父类
package lishidaihuan; class TangSeng { public void QuJing() { System.out.println("施主,贫僧自东土大唐而来,去往西天拜佛求经!"); } } class TuDi extends TangSeng { public void QuJing() { super.QuJing(); System.out.println("降妖除魔!"); } } public class LiShiDaiHuan { public static void main(String[] args) { // TODO Auto-generated method stub TangSeng tangSeng = new TangSeng(); System.out.println("唐僧:"); tangSeng.QuJing(); tangSeng = new TuDi(); System.out.println("徒弟:"); tangSeng.QuJing(); } }View Code
运行结果:
唐僧:
施主,贫僧自东土大唐而来,去往西天拜佛求经!
徒弟:
施主,贫僧自东土大唐而来,去往西天拜佛求经!
降妖除魔!
徒弟能替换师父!!!