Java 为什么我的大括号会出错?
守则:Java 为什么我的大括号会出错?,java,lwjgl,slick2d,Java,Lwjgl,Slick2d,守则: public class EmptyTile extends TileEntity{ //error on this brace try{ defaultTexture=TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("stone.png")); //defaultTexture is created in the class that this class extends
public class EmptyTile extends TileEntity{ //error on this brace
try{
defaultTexture=TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("stone.png")); //defaultTexture is created in the class that this class extends
}catch (IOException e) {
e.printStackTrace();
} //also an error on this brace
public EmptyTile(int x, int y, int height, int width, Texture texture) {
super(x, y, height, width, texture);
}
}
我还尝试将try/catch语句移动到EmptyTile构造函数,但它需要在调用超级构造函数之前初始化默认纹理,这显然是不允许的
我还尝试在这个类的父类中使defaultTexture变量既静态又常规
public class EmptyTile extends TileEntity{
public EmptyTile(int x, int y, int height, int width, Texture texture) {
super(x, y, height, width, texture);
try{
defaultTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("stone.png")); //defaultTexture is created in the class that this class extends
} catch (IOException e) {
e.printStackTrace();
}
}
}
请注意,如果
TileEntity
在构造函数中使用defaultTexture
,则必须修改构造函数以允许传入该构造函数。不能将try/catch
放在类级别,只能放在构造函数、方法或初始值设定项块中。这就是导致报告错误的原因。尝试在构造函数中移动代码,假设defaultTexture
是一个属性:
public class EmptyTile extends TileEntity {
public EmptyTile(int x, int y, int height, int width, Texture texture) {
super(x, y, height, width, texture);
try {
defaultTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("stone.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
但如果defaultTexture
是静态属性,则使用静态初始值设定项块:
public class EmptyTile extends TileEntity {
static {
try {
defaultTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("stone.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public EmptyTile(int x, int y, int height, int width, Texture texture) {
super(x, y, height, width, texture);
}
}
public class EmptyTile extends TileEntity {
// empty brace is instance initializer
{
try {
defaultTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("stone.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public EmptyTile(int x, int y, int height, int width, Texture texture) {
super(x, y, height, width, texture);
}
}
如果要在构造函数之外执行此操作,可以将其放在实例初始值设定项块中:
public class EmptyTile extends TileEntity {
static {
try {
defaultTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("stone.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public EmptyTile(int x, int y, int height, int width, Texture texture) {
super(x, y, height, width, texture);
}
}
public class EmptyTile extends TileEntity {
// empty brace is instance initializer
{
try {
defaultTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("stone.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public EmptyTile(int x, int y, int height, int width, Texture texture) {
super(x, y, height, width, texture);
}
}
尝试使用static{try{…是的,可能正在寻找静态初始值设定项。我不认为静态是需要的:“defaultTexture是在这个类扩展的类中创建的”。我认为它不是静态的。“你不能在类级别上放置try/catch”这不一定是真的,您可以在第二个示例中删除static关键字,它将成为实例初始化块。@Radiodef如果您继续阅读,它会说:“仅在构造函数、方法或初始值设定项块内部”。您描述的是初始值设定项块。但是,通过不指定和不显示示例,您可以得到答案(显然是意外地)意味着只有初始化程序块用于静态。