无法实例化类型 Image java?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18960758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot instantiate the type Image java?
提问by john brown
public Image images[] = new Image[20];
for(i=0; i<10; i++){
images[i]=new Image(getClass().getResource("/images/"+i+".jpg"));
}
I am trying to add images to array but it gives the error Cannot instantiate the type Image j
我正在尝试将图像添加到数组,但它给出了错误无法实例化类型 Image j
What can be the reason?
原因是什么?
回答by Reimeus
Abstract classes cant be instantiated directly. You could use ImageIO.read
which returns BufferedImage
, a sub-class of Image
抽象类不能直接实例化。你可以使用ImageIO.read
which returns BufferedImage
,一个子类Image
void loadImages() throws IOException {
for (int i = 0; i < 10; i++) {
images[i] = ImageIO.read(getClass().getResource("/images/" + i + ".jpg"));
}
}
回答by Julien
Image
is an abstract class and thus cannot be instancied.
You should use one of the class who extend Image
, like BufferedImage
or VolatileImage
.
Image
是一个抽象类,因此不能被实例化。您应该使用扩展类之一Image
,例如BufferedImage
or VolatileImage
。
Source: Javadoc of Image
来源:图像的 Javadoc