我正在将一些 OpenGL Nvidia C 示例移植到 jogl,我有以下内容(init
是 GLEventListener
所需的抽象方法之一:
public abstract class NvAppBase implements GLEventListener {
@Override
public void init(GLAutoDrawable drawable) {
initRendering(gl4);
}
public void initRendering(GL4 gl4) {
}
}
public abstract class NvSampleApp extends NvAppBase {
@Override
public void init(GLAutoDrawable drawable) {
baseInitRendering(gl4);
}
protected void baseInitRendering(GL4 gl4) {
initRendering(gl4);
}
@Override
public void initRendering(GL4 gl4) {
}
}
public class BindlessApp extends NvSampleApp{
@Override
public void initRendering(GL4 gl4) {
}
}
鉴于:
NvAppBase
根本不使用,所有示例(例如BindlessApp
)总是扩展NvSampleApp
- 我希望扩展
NvSampleApp
的类能够只看到(并覆盖)initRendering
而不是init
有没有比将 NvSampleApp
简单地作为 BindlessApp
中的变量更好的方法,例如这样?
public class BindlessApp {
private NvSampleApp sampleApp;
}
最佳答案
为此,您可以使用关键字 final
。
Writing Final Classes and Methods关于 Oracle Java 教程。
You can declare some or all of a class's methods final. You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. The Object class does this—a number of its methods are final.
关于Java,超父类(super class)的隐藏方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34918236/