Java - 无法扩展类?

时间:2011-05-09 15:18:51

标签: java bufferedimage

我正在尝试使用此代码扩展java.awt.image.BufferedImage

import java.awt.image.BufferedImage;

public class FSImage extends BufferedImage {

    public FSImage() {
        // empty constructor
    }
}

但是,我收到了一些错误:

no suitable constructor found for BufferedImage()
constructor java.awt.image.BufferedImage.BufferedImage(java.awt.image.ColorModel, java.awt.image.WritableRaster, boolean, java.util.Hashtable<?,?>) is not applicable (actual and formal argument lists differ in length)
...

我做错了什么?

8 个答案:

答案 0 :(得分:9)

“空构造函数”隐式调用nullary基类构造函数,BufferedImage中不存在这样的构造函数。您需要显式调用适当的基类构造函数:

public FSImage() {
    super(args); // replace "args" with actual arguments to BufferedImage()
    // other stuff
}

答案 1 :(得分:3)

当扩展一个类时,子类最终必须调用超类的一些构造函数(无论是直接还是通过其定义的最终调用超类构造函数的其他构造函数链接)。如何获取参数通常是通过实现具有相同参数的构造函数然后使用super传递它们来完成的。

BufferedImage(int width, int height, int imageType)

是BufferedImage()的构造函数之一。由于您正在扩展它,您可以提供此构造函数。

FSImage(int width, int height, int imageType) 

然后调用super()调用超类的构造函数:

FSImage(int width, int height, int imageType) {
  super( width, height, imageType );
}

但是应该注意,只要调用有效的super()构造函数,您自己的构造函数就不需要具有相同的签名。例如,以下是合法的构造函数:

FSImage() {
  super( 100, 100, TYPE_INT_RGB );
}

如果没有定义任何构造函数,编译器将默认调用超类的无参数默认构造函数。因为在这种情况下,它不存在,您必须调用现有的构造函数。

答案 2 :(得分:1)

BufferedImage没有空构造函数。扩展它时,派生类需要在其超类(BufferedImage)中调用特定的构造函数。

答案 3 :(得分:1)

BufferedImage中没有定义默认构造函数,因此您无法执行

new BufferedImage();

以类似的方式,如果你为BufferedImage创建一个子类,它就不能在不满足初始化要求的情况下与它的超类接口。因此,子类构造函数必须至少调用一个超类构造函数。

你可以试试这个..

   public FSImage(int arg0, int arg1, int arg2) {
    super(arg0, arg1, arg2);

}

public FSImage() {
    super(100, 100, BufferedImage.TYPE_INT_ARGB);

}

答案 4 :(得分:0)

因为BufferedImage没有任何参数构造函数。

答案 5 :(得分:0)

您必须为要扩展的类实现一个或多个现有构造函数

http://download.oracle.com/javase/1.5.0/docs/api/java/awt/image/BufferedImage.html

答案 6 :(得分:0)

看起来你要扩展的类有一些必需的构造函数参数。这个类似的问题有你需要使用的语法来扩展类:

Java extending class with the constructor of main class has parameter

答案 7 :(得分:0)

BufferedImage没有没有arg构造函数,你需要在构造函数中使用相应的参数(需要图像的大小和内部存储方式的类型)进行super()调用。

相关问题