我有以下代码,因为我理解了<T extends Buffer>
真正的含义,所以我今天刚刚重构了它,这是简化版本:
public class Buffer {
protected final int bufferType;
protected final int bufferDataType;
protected int bufferId;
protected boolean created;
public Buffer(final int bufferType, final int bufferDataType) {
this.bufferType = bufferType;
this.bufferDataType = bufferDataType;
}
public <T extends Buffer> T create() {
assertNotCreated();
bufferId = GL15.glGenBuffers();
created = true;
return (T)this;
}
public boolean hasBeenCreated() {
return created;
}
private void assertNotCreated() {
if (hasBeenCreated()) {
throw new RuntimeException("Buffer has been created already.");
}
}
}
public class ArrayBuffer extends Buffer {
public ArrayBuffer(final int bufferDataType) {
super(GL15.GL_ARRAY_BUFFER, bufferDataType);
}
}
public class DynamicDrawArrayBuffer extends ArrayBuffer {
public DynamicDrawArrayBuffer() {
super(GL15.GL_DYNAMIC_DRAW);
}
}
警告发生在Buffer.create()
,抑制警告是否安全?有什么办法让它更安全吗?
另一个要求是不应在此 API 的调用/使用代码中添加任何混乱,具体而言,这意味着DynamicDrawArrayBuffer
可能不会附加泛型。