我认为您的意思不是搜索顺序.相反,我认为该规则适用:
6.5.5 Meaning of Type Names The meaning of a name classified as a
TypeName is determined as follows.
6.5.5.1 Simple Type Names If a type name consists of a single Identifier,
then the identifier must occur in the
scope of exactly one visible
declaration of a type with this name,
or a compile-time error occurs. The
meaning of the type name is that type.
在这种情况下,进口算作声明;参见JLS 6.1.但是,复杂的因素是影子规则(JLS 6.3.1),该规则说某些类型的声明(在这种情况下为类型)隐藏了现有的声明,而其他则没有.具体来说,“按需导入”(例如import java.util.*;或java.lang.*的隐式导入)不会遮盖其他声明.
例如
package foo;
import java.sql.Date;
import java.util.*; // import on demand: java.util.Date does not shadow java.sql.Date
import java.awt.*; // import on demand: java.awt.List does not shadow java.util.List
class Integer { // (foo.)Integer shadows java.lang.Integer
...
static class Integer { // (foo.Integer.)Integer shadows foo.Integer.
...
List t = ... // Compilation error, because there are currently two visible
// declarations of List (JLS 6.5.5.1)
}
}
另一个问题是,“单一类型导入”(如上面的import java.sql.Date;)会遮盖在同一包中声明的类型,并按需导入类型,但不会遮蔽通过另一个“单一”导入的其他类型. -type-import”(JLS 6.3.1).因此,例如,以下是编译错误:
import java.sql.Date;
import java.util.Date; // Compilation error