考虑一下:
public class Model{
private Map<Vector, Vector> vertices;
public Model(Vector v){
vertices.put(v, v);
}
}
我期待NPE,因为顶点是未初始化的;至少我期待一个错误,因为Map是抽象的,我正在使用一个对象。 有人可以在这里曝光吗?
编辑:
public class World{
public static void init(){
Model cube = new Model(someVector);
}
}
我有一个包含main()的Main类;在main()中我正在呼叫World.init();
为了便于阅读,简化了代码 编辑1:
public class Model extends Positionable{
public static Map<String, Model> map = new HashMap<>();
private Map<Vector3f, Vector3f> vertsAndNormals;
private Set<Face> faces;
public Model(String name_, Map<Vector3f, Vector3f> vertsAndNormals_){
super(); // \!/ passing `this`; may not have been entirely initialized
vertsAndNormals = new HashMap<>(vertsAndNormals_);
map.put(name_, this);
}
public Model(String name_, Set<Vector3f> vertices_){
super(); // \!/ passing `this`; may not have been entirely initialized
for(Vector3f vertex : vertices_)
vertsAndNormals.put(vertex, new Vector3f(0, 0, 0)); // \!/ why does this NOT cause an NPE?
map.put(name_, this);
}
public Model(String name_){
this(name_, new HashMap<Vector3f, Vector3f>());
}
}
我打电话的地方:
public class World{
public static Set<Model> modelsInWorld = new HashSet<>();
public static void init(){
Model cube = new Model("gugu");
}
}
在main()中:
World.init();
编辑2:
public abstract class Positionable{
public static Set<Positionable> set = new HashSet<>();
public float x = 0;
public float y = 0;
public float z = 0;
public float xRol = 0;
public float yPit = 0;
public float zYaw = 0;
public Positionable(){
set.add(this);
}
}
答案 0 :(得分:0)
您实际上并没有调用具有您关注的行的构造函数。您调用带有String
的构造函数,该构造函数转发到带有String
和Map
的构造函数。采用Set
的构造函数将在运行时导致NPE。