英文:
How can I use the function of the grandfather class in Java? I try to use ''super.super'' but failed
问题
使用“super”关键字,我们都知道可以调用父类的函数,但如何使用祖父类的函数呢?使用“super.super”没有效果。我是一个新的Java学习者,感谢您的回复。
英文:
Well, we all know that we can use the function of father class with "super", but how to use a function of grandfather class? Using "super.super" has no effects. I'm a new Java learner, thanks for your response.
答案1
得分: 1
你所询问的是不可能的,这在Java语言中是不允许的(尽管JVM允许)。
一种方法是在父类中也添加一个super调用,这样从子类调用super将调用父类的super,从而调用祖父类中所需的方法。这是我想到的方法。
如果你不想修改父类,并且只想在子类中找到解决方案,那是不可能的。我建议修改你的类设计。
英文:
What you are asking is not possible, it is not allowed by Java language (though allowed by JVM).
One way would be to add a super call in your parent as well, so calling super from child, will call super of parent which will call the method you need in grandparent. Thats what came to my mind.
If you do not want to touch your parent class, and want a solution only in child class, it is not possible. I would suggest changing your class designs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论