对象应该可以替换为其子类型,而不影响代码的正确性
让我们用继承来理解这一点(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());
}
}
登录后复制
这是一个已经被曝光的设计,因为 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());
}
}
登录后复制
**打破层次结构:如果替换失败,则打破层次结构
解决方案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
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.add(p1);
products.add(p2);
products.add(p2);
for(Product product : products){
System.out.println(product.getDiscount());
}
}
告诉不要问:这里我们告诉 utils 类打印所有折扣,并且 utils 类不必询问任何内容(因为它之前通过 if 语句询问)
以上就是里氏替换原则的详细内容,更多请关注php中文网其它相关文章!
AmandaIncaboraa2 个月前
发表在:关于我们"我很想找出激励你的东西。 和我聊天 h...
AmandaIncabora22 个月前
发表在:关于我们我在等你的留言! 过来打个招呼! ...
AmandaIncaborac2 个月前
发表在:关于我们让我们今晚难忘...你的地方还是我的? ...
BryanDen3 个月前
发表在:关于我们Самый быстрый и безо...
91资源网站长-冰晨8 个月前
发表在:【账号直充】爱奇艺黄金VIP会员『1个月』官方直充丨立即到账丨24小时全天秒单!不错不错,价格比官方便宜
91资源网站长-冰晨8 个月前
发表在:2022零基础Java入门视频课程不错,学习一下