我想问一下是否有更优雅的方式来编写以下代码:
public class MTree {
class MTreeObject {
double[] vector;
}
class RoutingObject extends MTreeObject {
double radius;
}
class LeafObject extends MTreeObject {
int id;
}
class Node {
LinkedList<MTreeObject> list;
}
class InnerNode extends Node {
LinkedList<RoutingObject> list;
}
class LeafNode extends Node {
LinkedList<LeafObject> list;
}
public static void main(String[] argv) {
MTreeObject object = new RoutingObject();
Node node = new InnerNode();
((InnerNode)node).list.add((RoutingObject)object);
}
}
问题是,如果我有例如:
class Result {
Node node;
}
然后调用 main
public static void main(String[] argv) {
Result result = new Result();
MTreeObject object = new RoutingObject();
result.node = new InnerNode();
result.((InnerNode)node).list.add((RoutingObject)object);
}
它不按我想要的方式工作。那么我也可以这样做:
newnode = result.node
((InnerNode)newnode).list.add((RoutingObject)object);
但是在我更复杂的代码中我会有太多的变量......