What Rees Replacement Principle
Definition 1: If for every object o1 of type S, there is object O2 of type T, in program P, o1 of type T can be replaced by o2, and the behavior of program P has not changed, then type S is a subtype of type T.
Definition 2: All places where base classes are applied must be able to transparently use objects of their subclasses.
Both definitions are acceptable, but the second one is more complicated to read, and the second one is more concise. Personally, I recommend the definition of multiple products and one product.
Rees Replacement Rule
- Other classes should rely on parent classes or interfaces
//Student
class Student{
public String name;
}
//Teacher
class Teacher{
//sign up
public static void signUp(Student student){
System.out.println("Currently enrolled student name:"+student.name);
}
}
//Student Zhang San
class ZhangSan extends Student{
}
class TeacherWang{
//sign up
public static void signUp(ZhangSan zhangSan){
System.out.println("Currently enrolled student name:"+zhangSan.name);
}
}
Looking at the difference above, Teacher relies on Student while Teacher Wang relies on Zhang San. Teacher Wang does not conform to the LSP principle. Of course, we do not always use this principle when designing programs. Sometimes we also rely directly on subclasses. This depends on specific requirements.
2. Subclasses must be completely parent-class methods
Class Student{
//Obtaining Student Card
public abstract String getStudentID();
}
Class ZhangSan extends Student{
public String getStudentID(){
return null;
}
}
We see Zhang San is a subclass of Student, but there is no implementation of getStudent ID () method. If the school gatekeeper lets you enter the campus according to the student ID, if there is no student ID, then you are not qualified to enter the campus, which is equivalent to not a student.
Subclasses can have methods and attributes different from parent classes
Zhang San and LiSi are all students, so they have all the attributes and methods of Student, but Zhang San likes playing chess, LiSi likes playing basketball, so that is also possible, then the converse is not true, as long as the Student likes playing basketball and chess?Method of overloading parent class, parameter type >= overloaded parameter or parameter type!= overloaded parameter
//Parent class
class Parent {
public void test(HashMap hashMap){}
}
//Subclass
class Child extends Parent{
@Override
public void test(HashMap hashMap){}
public void test(Map map){}
public void test(String text){}
}
//Program P
class Test{
public static void main(String[] args){
//Using parent class
Parent parent = new Parent();
HashMap hashMap = new HashMap();
parent.test(hashMap);
//Subclass substitution
//Child child = new Child ();
//HashMap hashMap = new HashMap();
//child.test(hashMap);
}
}
//According to Definition 1, let's replace Parent with Child, and finally call the method in Parent.
Is it right to look at the other way around?
//Parent class
class Parent {
public void test(Map map){}
}
//Subclass
class Child extends Parent{
public void test(HashMap hashMap){}
@Override
public void test(Map map){}
public void test(String text){}
}
//Program P
class Test{
public static void main(String[] args){
//Using parent class
Parent parent = new Parent();
HashMap hashMap = new HashMap();
parent.test(hashMap);
//Subclass substitution
//Child child = new Child ();
//HashMap hashMap = new HashMap();
//child.test(hashMap);
}
}
//After replacing, it was found that the child invoked not the parent's test (Map) method, but its own test(HashMap hashMap) method, which did not meet the definition.1,Unimpression Program after Replacement P Function.