为什么Java编译器不支持导入的继承? [英] Why should the Java compiler not support inheritance of imports?

查看:120
本文介绍了为什么Java编译器不支持导入的继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,导入与(外部)类相关,因为每个(外部)类都应该在单独的文件中编码。因此,可以声明在类定义之前的 import ...; 指令与类相关联(有点类似于注释)。

In Java, imports are related to an (outer) class, as every (outer) class is supposed to be coded in a separate file. Thus, one could claim that the import ...; directives before a class definition are associated with the class (somewhat like annotations are).

现在,如果可以继承一个父类'imports',这将大大减少源文件的混乱。为什么这不可能?即为什么Java编译器不考虑基类的导入?

Now, if one could inherit a parent class' imports, that would greatly reduce the clutter of source files. Why should this not be possible? i.e. why should the Java compiler not consider also the imports of base classes?

注意:


  • 可能有不止一个答案。

  • 我知道这不是一个问题,如果你让eclipse组织你的导入,无需提及。这是关于为什么,而不是如何(a-la- this )。

  • There's probably more than one answer.
  • I know this is not much of an issue if you let eclipse organize your imports, no need to mention that. This is about the 'why', not the 'how' (a-la-this).

推荐答案

首先,需要注意的是,不是每个类都必须在单独的文件中编码 - 每个公开,顶级类必须是。没有,导入并不真正与任何类相关联 - 它们只是用于在文件中包含某些外部类/包,以便可以使用它们的语句。事实上,你从来不需要实际导入任何东西,你总是可以使用全名,例如:

Firstly, it is important to note that not every class must be coded in a separate file - but rather that every public, top level class must be. And no, imports are not really associated with any class - they are just statements used to include certain external classes / packages within a file so that they can be used. In fact, you never need to actually import anything, you can always use the full name, i.e.:

java.util.List<String> list = new java.util.ArrayList<String>();

导入是为了方便起见(只有编译器 - 它们在类编译后丢失) ,保存你不必写出所有的,而只让你写 List< String> list = new ArrayList< String> (从 java.util 中进行相关导入之后)。因此,没有理由为什么子类应该'继承'导入。

Imports are there for convenience (and only for the compiler - they are lost after the class is compiled), to save you from having to write all that out and instead only make you write List<String> list = new ArrayList<String> (after you make the relevant imports from java.util). Consequently, there is no reason why subclasses should 'inherit' imports.

这篇关于为什么Java编译器不支持导入的继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆