您的位置:首页 > 编程语言 > Java开发

Java的super调用案例: super.getClass()返回的是子类自己

2015-05-28 23:44 471 查看
If you override a method from your superclass (or your superclass's superclass etc.), super.theMethod() will invoke the original method instead of the one you overrode it with. If you did not actual override theMethod, super.theMethod() will act exactly like theMethod().

In this case I assume you did not override getClass() (in fact I know you didn't because it's final), so super.getClass() acts exactly like getClass(), i.e. either way the getClass method of the Object class is called.

It is, when you call getClass, you're calling the method getClass defined in Object. Object is a superclass of your class (even if not the direct superclass), so you are calling the superclass's version of the method. In fact the superclass's version is the only version of that method

如果你要显示父类, 要使用

this.getClass().getSuperclass()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: