覆盖而不调用父方法,这违反了Liskov原理 [英] Overriding without calling parent method, violating Liskov Principle

查看:75
本文介绍了覆盖而不调用父方法,这违反了Liskov原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的项目.而且我在项目中与Liskov Principle的含义冲突.

I am developing a simple project. And I have a conflict with meaning of Liskov Principle in my project.

我通过以下示例简化了我的项目问题:

I simplified my question of my project with this example:

public class Animal {  

     public void feed() {     

         // do something here         
     }    
}    


public class Dog extends Animal {

    // some methods and attributes

    @Override
    public void feed() {   

        // never call parent feed() method (super.feed())
    }
}

所以,我的问题是,如果我不调用父方法并在重写方法中编写全新的代码,这是否违反了《里斯科夫原理》?

So, my question is, if I don't call parent method and write a completely new codes in override method, Is this violates Liskov Principle?

谢谢.

推荐答案

不,只要子类的实现满足基类的期望,它就不会违反Liskov原理.

No, it will not violate the Liskov Principle as long as the subclass' implementation satisfies the expectations of the base class.

这篇关于覆盖而不调用父方法,这违反了Liskov原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆