在Java中,使用“super”表示关键字,我必须导入目标类吗?
当在构造函数中使用 super 关键字时,是否必须导入 super 引用的类(当 super 不引用 Object 时)?
class A extends ... {
A() {
super(); // do we need to import the class super refers to?
}
}
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)?
class A extends ... {
A() {
super(); // do we need to import the class super refers to?
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,因为它位于
extends
子句中。super() 本身不需要导入,但要使其有意义,您需要一个超类。当然,如果它来自
java.lang
,则不需要导入它Yes, because it is in the
extends
clause.The
super()
itself requires no imports, but for it to make sense you need a superclass. You don't need to import it, of course, if it is fromjava.lang
如果超类不在同一个包中或在 java.lang 中,则确实需要导入它。如果基类不可用,
super()
无论如何都不起作用。You do need to import the super class if it is not in the same package or it is in java.lang. If the base class is not available,
super()
doesn't work anyway.