简体   繁体   English

我应该如何引用 Java 中“祖父”类的函数/void?

[英]How should I reference to a function/void of the "grandparent" class in Java?

I know that I can reference to a function/void of my parent class with super.我知道我可以使用 super 引用父类的函数/void。 But what about my grandparent?但是我的祖父母呢? super.super.void/function? super.super.void/功能?

As long as your class and your parent class don't overwrit it you can simply call it.只要您的班级和您的父班级不覆盖它,您就可以简单地调用它。 If it has the right visibility (protected,public or if your subclass is in the same package even package-private)如果它具有正确的可见性(protected、public 或者如果你的子类在同一个包中,甚至包私有)

Let's say we have something like this:假设我们有这样的事情:
A extends B which extends C and C has a method .method() A 扩展 B 扩展 C 和 C 有一个方法.method()

So, in this case, B has inherit all the C's methods, and so if A wants to call a method that is in C, it just need to call super.method() , which will call B.method() , which (if it's not been overwritten) is the method that is being inherited by C (obviously check the visibility)所以,在这种情况下,B 已经继承了 C 的所有方法,所以如果 A 想要调用 C 中的方法,它只需要调用super.method() ,它会调用B.method() ,其中 (如果它没有被覆盖)是 C 继承的方法(显然检查可见性)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM