里氏替换原则(Liskov Substitution Principle,LSP)
里氏替换原则(Liskov Substitution Principle,LSP)
- If for each object 01 of type S there is a object 02 of type T such that for all program P define in terms of T, the behavior of P is unchanged where o1 substitude o2, then S is a subtype of T.(对于每一个类型是S的对象,存在一个类型为T的对象,使得以T定义的程序P所在的对象由o2替换为o1时,行为不发生任何变化。那么S就是T的子类)
- Function that use pointers or refererence to base classes must be able to use objects of derived classes without knowing it.(使用基类的地方必须能透明的使用子类)
- 子类必须完全实现所有父类的方法
- 子类可以有自己的特性
- 覆盖或者实现父类的方法的时参数可以被放大
- (可以理解为父类的入参类型是子类入参的子类)
- 注意:
- 尽量的不要子类太过于个性化,以免后期维护复杂。