里氏替换原则

里氏替换原则

对象应该可以替换为其子类型,而不影响%ign%ignore_a_1%re_a_1%码的正确性

让我们用继承来理解这一点(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 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());    }}

这是一个已经被曝光的设计,因为 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 vehicles = new arraylist();    vehicles.add(vehicle1);    vehicles.add(vehicle2);    vehicles.add(vehicle3);    for(vehicle vehicle : vehicles){        system.out.println(vehicle.getinteriorwidth());    } }

**打破层次结构:如果替换失败,则打破层次结构

解决方案2:告诉不要问

Waymark Waymark

Waymark是一个视频制作工具,帮助企业快速轻松地制作高影响力的广告。

Waymark 79 查看详情 Waymark

我们再举一个亚马逊的例子
亚马逊为所有第三方产品提供 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 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());    }}

请注意,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 products = new ArrayList();    products.add(p1);    products.add(p2);    products.add(p2);    for(Product product : products){        System.out.println(product.getDiscount());    }}

告诉不要问:这里我们告诉 utils 类打印所有折扣,并且 utils 类不必询问任何内容(因为它之前通过 if 语句询问)

以上就是里氏替换原则的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1010649.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月2日 00:01:15
下一篇 2025年12月2日 00:01:36

相关推荐

发表回复

登录后才能评论
关注微信