你如何在Java中使用父类的函数?我尝试使用’super.super’,但失败了。

huangapple go评论57阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年8月8日 11:02:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63311412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定