我在同一个包中创建了如下类test
。这只是为了测试java是否真的允许它。它确实如此,但随之而来的是问题。
package test;
public class E {
}
package test;
public class T {
}
package test;
import test.E;
import test.T;
public class K<E, T> {
E e;
T t;
public K(E e, T t) {
this.e = e;
this.t = t;
}
public static void main(String[] args) {
K k = new K<E, T>(new E(), new T());
}
}
上面的代码给出了多个编译问题
Multiple markers at this line
- Cannot make a static reference to the non-static type E
- Cannot make a static reference to the non-static type T
- Cannot make a static reference to the non-static type T
- Cannot make a static reference to the non-static type E
- K is a raw type. References to generic type K<E,T> should be
parameterized
它清楚地表明编译器在 E 和 E 类之间对 T 相同。
所以解决方法是使用包定义它的真实类型。
K k = new K<test.E, test.T>(new test.E(), new test.T());
现在,如果所有这些类都在default package
其中,则无法解决此编译问题。所以问题是java是否应该允许在中声明这些类default package
?