简体   繁体   English

在Java中,要使用“super”关键字,我是否必须导入目标类?

[英]In Java, to use the “super” keyword, do I have to import the target class?

When, in a constructor, we use the super keyword, do we have to import the class the super refers to (when super doesn't refer to Object)? 在构造函数中,当我们使用super关键字时,我们是否必须导入super引用的类(当super不引用Object时)?

class A extends ... {
    A() {
        super(); // do we need to import the class super refers to?
    }
}

Yes, because it is in the extends clause. 是的,因为它在extends子句中。

The super() itself requires no imports, but for it to make sense you need a superclass. super()本身不需要导入,但要有意义,你需要一个超类。 You don't need to import it, of course, if it is from java.lang 当然,如果是来自java.lang ,则无需导入它

You do need to import the super class if it is not in the same package or it is in java.lang. 如果超类不在同一个包中,或者它在java.lang中,则需要导入超类。 If the base class is not available, super() doesn't work anyway. 如果基类不可用,则super()无论如何都不起作用。

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

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