在java中覆盖和隐藏有什么区别? [英] What's the difference between override and hidden in java?

查看:423
本文介绍了在java中覆盖和隐藏有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多。它们之间的区别在于,override是针对实例方法的,而hidden是针对静态方法的。隐藏实际上是对方法的重新定义。但是我仍然没有得到它。如果重新定义意味着父类的静态方法仍然存在于子类中,那只是我们看不到它?或者为什么我们把它称为隐藏而不是任何其他词?但如果它存在,我找不到再次调用该方法的方法。说实话,从功能层面来说,我无法找到它们与众不同的原因。有人可以从更深层次的内存中解释它吗?

I searched a lot. The difference between them is that override is for the instance method and hidden is for the static method. And the hidden is in fact the redefinition of the method. But I still don't get it.If redefinition means that the static method of parent still exists in the subclass, it is just we can't see it? Or why we call it hidden but not any other words? But if it exists, I can't find a way to call the method again. To be honest from a function level I can't find why they are different. Can some one explain it from a deeper level such as memory?

推荐答案

静态成员(方法和变量)不会出现在继承它们的子类(Child类)对象,但它们将作为单个副本存在于内存中。

Static members(methods and variables) will not be present in the sub class(Child class) object which inherit them but they'll be present as a single copy in the memory.

静态成员可以通过类名访问超类和子类,但它们实际上并不存在于这些类的对象中。

Static members can be accessed by the class name of both Super class and sub class but they are not physically present in the object of these classes.

当你继承非静态成员时,内存中的Sub类对象将是包含继承的方法以及它自己的方法。因此,当您尝试在此处编写类似的方法时,将覆盖超类方法。另一方面,由于静态方法不参与继承,所以您编写的任何类似方法都存在于超类中,新方法将在每次要求时运行。父类方法只是隐藏但没有被覆盖!

Where as when you inherit non-static members, Sub class object in memory will contain both inherited methods as well as the methods of its own. So when you try to write a similar method here, super class method will be overridden. On the other hand as static methods does not participate in inheritance, any similar method you write that is present in super class, new method will run every-time it is asked for. Parent class method is just hidden but not overridden!

这篇关于在java中覆盖和隐藏有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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