[英]Java Inner class compilation error
I'm trying to instantiate a public non-static inner class but I keep getting a compilation error. 我正在尝试实例化一个公共的非静态内部类,但是我不断收到编译错误。
I have 2 classes: a tree class and a main class. 我有2类:树类和主类。
I'm ultimately trying to instantiate an array of points, but for the sake of this question, I'm trying to just instantiate a Point object. 我最终试图实例化一个点数组,但出于这个问题,我试图实例化一个Point对象。
The compilation error says that 编译错误说
Point can't be resolved to a type
点不能解析为类型
What am I doing wrong? 我究竟做错了什么?
Tree.java 树.java
public class Tree<T> {
public class Point<T> {
public T position[];
}
}
Main.java Main.java
public class Main {
public static void main(String args[]) {
Point<Double> point = new Tree<Point<Double>>().new Point<Double>();
}
}
Just leave out the T parameter when declaring the inner class. 声明内部类时,只需忽略T参数。
Just use T inside, but don't try to redefine a type parameter with the same name T. 只需在内部使用T,但不要尝试重新定义具有相同名称T的类型参数。
As suggested in the comments: 如评论中所建议:
Tree<Double>.Point<Double> point = new Tree<Double>().new Point<Double>();
but it looks like a little bit too redundant don't you think? 但是看起来有点多余吗?
you can remove the generic part of point 您可以删除点的通用部分
public class Tree<T> {
public class Point {
public T position[];
}
}
Tree<Double>.Point point = new Tree<Double>().new Point();
I suspect you don't want to have two different type parameters for Tree
and Point
. 我怀疑您不想为
Tree
和Point
使用两个不同的类型参数。 The way you wrote it, the T
of Point
shades the T
of Tree
, so the parameters are actually different. 编写方式,
Point
of T
遮蔽Tree
T
,因此参数实际上是不同的。 If you want them to be the same, remove the T
of the inner class. 如果希望它们相同,则删除内部类的
T
This way the T
of outer class can be used in the inner class as well. 这样,外部类的
T
也可以在内部类中使用。
public class Tree<T> {
public class Point {
public T position[];
}
public static void main(String args[]) {
Tree<Double>.Point point = new Tree<Double>().new Point();
}
}
If you really want to have two different T
s, you can instantiate a Point
like this. 如果您确实希望拥有两个不同的
T
,则可以实例化一个Point
。 But then I highly recommend to rename one of the type parameters to prevent confusion. 但是,我强烈建议重命名类型参数之一以防止混淆。
Tree<Object>.Point<Double> point = new Tree<>().new Point<>();
Edit : According to the comments: 编辑 :根据评论:
Tree<Tree<?>.Point<Double>>.Point<Double> point = new Tree<Tree<?>.Point<Double>>().new Point<>();
Edit : According to further comments 编辑 :根据进一步的评论
Since you want a Tree
to contain always Points, there is no use in using generics here. 由于您希望
Tree
始终包含点,因此在此处使用泛型没有用。 It is a little bit dependent on what you want to do with this class, whether to remove the type parameter of Tree
of the one of Point
. 是否删除
Point
之一的Tree
的类型参数取决于您对此类的处理方式。
Possibility 1 removing type parameter of Tree
可能性1:删除
Tree
类型参数
public class Tree {
public class Point<T> {
public T position[];
}
}
You can go with that if you want to return a Point
from the methods in the Tree
如果要从
Tree
的方法返回Point
,可以使用该方法
Possibility 2 removing type parameter of Point
可能性2移除
Point
类型参数
public class Tree<T> {
public class Point {
public T position[];
}
}
You should go with that if you want the methods in Tree
to return objects of type T
如果要让
Tree
的方法返回T
类型的对象,则应该这样做
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.