【学习点滴-面向对象】面向对象的困境--正方形不能继承长方形?
2012-03-20 18:03
399 查看
面向对象方法的困境--正方形不能继承自长方形?
转摘:http://blog.csdn.net/javayuan/article/details/1191751
面向对象理论诞生之初,由于没有最佳实践的指导,往往导致继承的滥用。一个很著名的例子就是java类库中的堆栈类Stack继承自向量类Vector。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Stack extends Vector ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Object push(Object item) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addElement(item);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return item;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public synchronized Object pop() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
仅仅是为了重用Vector的管理元素的方法,就让Stack继承自Vector,导致了一个蹩脚的设计。人们后来总结出一条规律:优先使用组合而不是继承,只有当类A确实"is a"类B的时候,才使用继承。Stack不是Vector,所以这个时候应该使用组合。示例代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Stack...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Vector vector;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Object push(Object item) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vector.addElement(item);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return item;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
只有符合"is a"关系时才能使用继承,这条规律是符合我们的直觉的。面向对象技术就是用软件里面的对象模拟现实中的对象。如果狗不是猫,当然不能让狗继承自猫。但是实际的软件开发中,人们很快发现有些情况下即使类A确实"is a"类B,也不能使用继承。最著名的一个例子就是正方形不能继承自长方形了,很多讲面向对象设计的书里都有。这是一个明显违反直觉的例子,示例代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Rectangle ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Rectangle(double width,double height)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.width=width;this.height=height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getHeight() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setHeight(double height) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.height = height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getWidth() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setWidth(double width) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.width = width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Square extends Rectangle...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setHeight(double height) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.setHeight(height);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.setWidth(height);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setWidth(double width) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.setHeight(width);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.setWidth(width);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
因为Rectangle类中的setHeight和setWidth方法对Square类不合适,为了保证正方形的长和宽相等,改写了这两个方法。上面的代码初看上去很合理,其实有问题。这个代码的缺陷我就不详细讨论了,很多书都对其做了详细的分析。我只是从理论上指出其不合理之处。根据契约式设计,有下面的原则:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
子类的前置条件不能强于父类的前置条件。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
子类的后置条件不能弱于父类的后置条件。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
子类的类不变式不能弱于父类的类不变式。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
这个原则也可以由Liskov替换原则得来。比方说要能够把父类替换成子类,那么父类所接受的任何参数,子类也必须能接受,也就是子类的前置条件不能强于父类的前置条件。
对于长方形类的setHeight方法,其前置条件是height>0,其后置条件是this.height==height&&this.width==old.width。对于正方形类的setHeight方法,其前置条件也是height>0,不强于父类的前置条件。但是其后置条件变成this.height==height&&this.width==height。它违背了父类的后置条件。子类的后置条件不能弱于父类的后置条件的意思是子类必需遵守父类的后置条件,同时还可以加上自己的更强的后置条件。
对于上面这种反常的情况,人们提出了子类型的概念。上面的代码中Square类是Rectangle类的子类(subclass),但是不是子类型(subtype)。因为它违反了Liskov替换原则。这个概念的引入应该说是有很大的混淆性。
那么到底为什么会出现这种反常的情况,正方形就是长方形,使用继承是合情合理的。如果一个设计方法会出现太多的例外情况,那它肯定不是一种好的设计方法。
老师:同学们请看!这是一个正方形,现在我让它的垂直边高度增加,现在成为什么形状了?
学生:报告老师,是长方形。
老师:回答正确。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
我们看到在现实中完全可以单独改变正方形的边长,这时改变以后的形状不再是正方形了。通过和现实情况对比,我们受到了启发。那就是正方形和长方形应该是不可变类。当它的边长改变以后,就变成了一个新的长方形。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Rectangle ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Rectangle(double width,double height)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.width=width;this.height=height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getHeight() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getWidth() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Rectangle changeHeight(double height) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Rectangle(this.width,height);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Rectangle changeWidth(double width) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Rectangle(width,this.height);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Square extends Rectangle ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Square(double dege) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super(dege, dege);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Square changeEdge(double edge)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Square(edge);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
通过这种思路,我们就得到了一个完全和现实对象相吻合的设计。所以说并不是正方形不能继承自长方形。
转摘:http://blog.csdn.net/javayuan/article/details/1191751
面向对象理论诞生之初,由于没有最佳实践的指导,往往导致继承的滥用。一个很著名的例子就是java类库中的堆栈类Stack继承自向量类Vector。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Stack extends Vector ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Object push(Object item) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addElement(item);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return item;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public synchronized Object pop() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
仅仅是为了重用Vector的管理元素的方法,就让Stack继承自Vector,导致了一个蹩脚的设计。人们后来总结出一条规律:优先使用组合而不是继承,只有当类A确实"is a"类B的时候,才使用继承。Stack不是Vector,所以这个时候应该使用组合。示例代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Stack...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Vector vector;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Object push(Object item) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vector.addElement(item);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return item;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
只有符合"is a"关系时才能使用继承,这条规律是符合我们的直觉的。面向对象技术就是用软件里面的对象模拟现实中的对象。如果狗不是猫,当然不能让狗继承自猫。但是实际的软件开发中,人们很快发现有些情况下即使类A确实"is a"类B,也不能使用继承。最著名的一个例子就是正方形不能继承自长方形了,很多讲面向对象设计的书里都有。这是一个明显违反直觉的例子,示例代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Rectangle ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Rectangle(double width,double height)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.width=width;this.height=height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getHeight() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setHeight(double height) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.height = height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getWidth() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setWidth(double width) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.width = width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Square extends Rectangle...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setHeight(double height) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.setHeight(height);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.setWidth(height);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setWidth(double width) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.setHeight(width);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.setWidth(width);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
因为Rectangle类中的setHeight和setWidth方法对Square类不合适,为了保证正方形的长和宽相等,改写了这两个方法。上面的代码初看上去很合理,其实有问题。这个代码的缺陷我就不详细讨论了,很多书都对其做了详细的分析。我只是从理论上指出其不合理之处。根据契约式设计,有下面的原则:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
子类的前置条件不能强于父类的前置条件。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
子类的后置条件不能弱于父类的后置条件。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
子类的类不变式不能弱于父类的类不变式。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
这个原则也可以由Liskov替换原则得来。比方说要能够把父类替换成子类,那么父类所接受的任何参数,子类也必须能接受,也就是子类的前置条件不能强于父类的前置条件。
对于长方形类的setHeight方法,其前置条件是height>0,其后置条件是this.height==height&&this.width==old.width。对于正方形类的setHeight方法,其前置条件也是height>0,不强于父类的前置条件。但是其后置条件变成this.height==height&&this.width==height。它违背了父类的后置条件。子类的后置条件不能弱于父类的后置条件的意思是子类必需遵守父类的后置条件,同时还可以加上自己的更强的后置条件。
对于上面这种反常的情况,人们提出了子类型的概念。上面的代码中Square类是Rectangle类的子类(subclass),但是不是子类型(subtype)。因为它违反了Liskov替换原则。这个概念的引入应该说是有很大的混淆性。
那么到底为什么会出现这种反常的情况,正方形就是长方形,使用继承是合情合理的。如果一个设计方法会出现太多的例外情况,那它肯定不是一种好的设计方法。
老师:同学们请看!这是一个正方形,现在我让它的垂直边高度增加,现在成为什么形状了?
学生:报告老师,是长方形。
老师:回答正确。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
我们看到在现实中完全可以单独改变正方形的边长,这时改变以后的形状不再是正方形了。通过和现实情况对比,我们受到了启发。那就是正方形和长方形应该是不可变类。当它的边长改变以后,就变成了一个新的长方形。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Rectangle ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private double height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Rectangle(double width,double height)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.width=width;this.height=height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getHeight() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return height;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public double getWidth() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return width;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Rectangle changeHeight(double height) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Rectangle(this.width,height);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Rectangle changeWidth(double width) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Rectangle(width,this.height);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Square extends Rectangle ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Square(double dege) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super(dege, dege);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Square changeEdge(double edge)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Square(edge);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
通过这种思路,我们就得到了一个完全和现实对象相吻合的设计。所以说并不是正方形不能继承自长方形。
相关文章推荐
- 面向对象方法的困境--正方形不能继承自长方形?
- 从“Liskov替换原则”和“Refused Bequest”看“正方形为什么不能继承长方形”
- VC++入门经典学习笔记--继承和面向对象
- 《剑指Offer》学习笔记--面试题48:不能被继承的类
- 【IOS 开发学习总结-OC-16】★★objective-c面向对象之——类的继承
- php的学习笔记之面向对象(四)继承
- 黑马程序员学习笔记四——Java 面向对象 特点之 继承
- Objective-C基础学习笔记(四)-面向对象的三大特性之继承-self关键字
- PHP面向对象三大特点学习(充分理解抽象、封装、继承、多态)
- IOS学习之OC面向对象—继承
- js 学习面向对象的继承
- 【python学习笔记】Python面向对象的理解(封装,继承,多态)
- 黑马程序员-------面向对象的封装、继承、多态一些学习笔记
- Objective-C基础学习笔记(四)-面向对象的三大特性之继承
- JavaScript学习笔记6 面向对象之继承
- html5学习笔记---05.JavaScript 中的面向对象,继承和封装
- java学习之旅42--面向对象_15_继承_组合
- js设计模式学习之面向对象的javascript(三)--原型式继承
- 面向对象--继承学习。
- Java学习第六课--面向对象的三大特性(封装、继承和多态)