正方形继承长方形违反liskov原则?
几乎所有的将Liskov原则的文章都提到了“正方形继承长方形违反Liskov原则”。
但都说的不清不楚的
假设我有如下的两个类,请问哪个地方违反了Liskov原则呢?
public class Rectangle ...{
protected double width;
protected double height;
public double getWidth() ...{
return width;
}
public void setWidth(double width) ...{
this.width = width;
}
public double getHeight() ...{
return height;
}
public void setHeight(double height) ...{
this.height = height;
}
public double calcSize() ...{
return this.height * this.width;
}
}
public class Square extends Rectangle ...{
@Override
public void setHeight(double height) ...{
setEdge(height);
}
@Override
public void setWidth(double width) ...{
setEdge(width);
}
@Override
public double getHeight() ...{
return getEdge();
}
@Override
public double getWidth() ...{
return getEdge();
}
public double getEdge() ...{
return this.height;
}
public void setEdge(double edge) ...{
this.height = edge;
this.width = edge;
}
}