
对象应在不影响代码正确性的情况下,可以替换为其子类型
让我们用继承来理解这一点(is-a关系) 比如鸵鸟是鸟,驼背是车等等
示例:赛车是汽车
public class car{
public double getcabinwidth(){
//return cabin width
}
}
public class racingcar extends car{
@override
public double getcabinwidth(){
//unimplemented
}
public double getcockpitwidth(){
//return the cockpit width of the racing car
}
}
racingcar 覆盖汽车 getcabinwidth() 但保留它还没有实现 因为赛车没有驾驶室宽度(如果你看到一级方程式赛车,它没有内部空间,它只有一个驾驶员的驾驶舱) 所以赛车的内部空间叫驾驶舱。 注:赛车的一些规格可能与通用汽车不匹配
public class carutil{
car car1 = new car();
car car2 = new car();
car car3 = new racingcar();
list<car> mycars = new arraylist();
mycars.add(car1);
mycars.add(car2);
mycars.add(car3);
// this will not work in 3rd iteration, because the getcabinwidth() in racingcar is not implemented
for(car car : mycars){
system.out.println(car.getcabinwidth());
}
}
</car>
这是一个被曝光的设计,因为 for 在第三次迭代中,循环将失败。 为了解决这个问题,我们必须从根本上解决,那就是继承本身。
解决方案 1 :(打破层次结构)
我们必须打破继承,相反,我们将这样做 car 和 racingcar 提供一个共同的父亲对象
我们将创建一个非常常见的父类,名为 vehicle
public class vehicle{
public double getinteriorwidth(){
//return the interior width
}
}
public class car extends vehicle{
@override
public double getinteriorwidth(){
return this.getcabinwidth();
}
public double getcabinwidth(){
//return cabin width
}
}
public class racingcar extends vehicle{
@override
public double getinteriorwidth(){
return this.getcockpitwidth();
}
public double getcockpitwidth(){
//return the cockpit width of the racing car
}
}
public class vehicleutils{
vehicle vehicle1 = new car();
vehicle vehicle2 = new car();
vehicle vehicle2 = new racingcar();
list<vehicle> vehicles = new arraylist();
vehicles.add(vehicle1);
vehicles.add(vehicle2);
vehicles.add(vehicle3);
for(vehicle vehicle : vehicles){
system.out.println(vehicle.getinteriorwidth());
}
}
</vehicle>
**打破层次结构:如果替换失败,层次结构将被打破
解决方案2:告诉不要问
点击下载“修复打印机驱动工具”;
让我们再举一个亚马逊的例子 亚马逊为所有第三方产品提供服务 x 折扣。 为所有自营产品提供1.5倍x折扣(amazon basics产品均为亚马逊自营产品)
public class product{
public double discount = 20;//x amount of discount on all the third-party products on amazon
public double getdiscount(){
return discount;
}
}
public class inhouseproduct extends product{
public void applydiscount(){
discount = discount*1.5;// 1.5 times more discount on inhouseproducts
}
}
public class pricingutils{
product p1 = new product();
product p2 = new product();
product p2 = new inhouseproduct();
list<product> products = new arraylist();
products.add(p1);
products.add(p2);
products.add(p2);
for(product product : products){
if(product instanceof inhouseproduct){
((inhouseproduct)product).applydiscount();
}
system.out.println(product.getdiscount());
}
}
</product>
请注意,if 句子涉及更新 inhouseproduct 这违反了里氏替换原则的折扣金额(因为我们应该能够使用对象 product 替换为其子类型 inhouseproduct),但是在 if 我们手动更新句子中不应该做的折扣金额。
对 inhouseproduct 轻微修改类别可以解决这个问题
public class inhouseproduct extends product{
@override
public double getdiscount(){
applydiscount();
return discount;
}
public void applydiscount(){
discount = discount*1.5;
}
}
最后从 pricingutils 类中删除 if 语句
public class PricingUtils{
Product p1 = new Product();
Product p2 = new Product();
Product p2 = new InHouseProduct();
List<product> products = new ArrayList();
products.add(p1);
products.add(p2);
products.add(p2);
for(Product product : products){
System.out.println(product.getDiscount());
}
}
</product>
告诉不要问:我们在这里告诉你 utils 类打印所有折扣, utils 类别不需要询问任何内容(因为它以前是通过的 if 语句询问)
以上是里氏替代原则的详细内容。请关注图灵教育的其他相关文章!