Java覆盖如何工作 [英] How Does Java Overriding Work

查看:67
本文介绍了Java覆盖如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个愚蠢的困惑,当我们重写父类方法时,这个派生的重写方法是否仍然保留父类方法的代码,或者这是我们可以定义的新方法?

I have a stupid confusion, when we override the parent class method then does this derived overridden method still hold the code of parent class method, or it is a new fresh method which we can define?

推荐答案

阅读本文以明确概念. http://docs.oracle.com/javase/tutorial/java/IandI /override.html

Read this article to get concept clear. http://docs.oracle.com/javase/tutorial/java/IandI/override.html

通常,当我们想要扩展超类的方法或想要更改完整的逻辑时,我们会这样做.

Generally we do when we want to extend the method of super class or to want to change the complete logic.

例如,:超类具有使用冒泡排序的排序方法.

For ex: Super class have sorting method which use bubble sort.

在派生类中,您想采用相同的方法,但要实现快速排序.然后,我们进行覆盖.

In Derived class you want to take same method but want to implement quick sort. Then we do overriding.

第二

如果您要先执行超类方法,则您的子类被覆盖的方法逻辑将使用super.methodname().

If you want to execute super class method first then your sub class overriden method logic then we use super.methodname().

最后一个问题,如果您覆盖该方法并且未像super.method()那样调用超类方法,则它并不表示其新鲜方法.这意味着我已经解释了排序示例.

Last to point of your question If you override the method and not called super class method like super.method() then its not mean its fresh method. Its means I already explain the sort example.

这篇关于Java覆盖如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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