它可能会违反信息隐藏信息隐藏虽然是一种很好的做法,但与 Liskov 替换原则几乎没有关系。(A) 子类应始终提供至少与其父类(类)相同的行为。这是真的,但通过禁止对继承成员使用更严格的访问修饰符来实现。一个较弱的访问修饰符表面附加行为。class A { private int lastInput; protected int getLastInput() { return lastInput; } public int getSquareValue(int input) { lastInput = input; return getLastInput()*getLastInput(); }}class B extends A { public int getLastInput() { return super.getLastInput(); }}A aa = new A();B bb = new B();A ab = bb;// All behaviors of A exist in B as well.// B can be substituted for A.System.out.println(aa.getSquareValue(5)); // 25System.out.println(ab.getSquareValue(5)); // 25// B also has new behaviors that A did not surface.// This does not prevent B from substituting for A.System.out.println(bb.getLastInput()); // 5