开发者

Java - cannot extend class?

I'm trying to extend java.awt.image.BufferedImage us开发者_如何学Cing this code:

import java.awt.image.BufferedImage;

public class FSImage extends BufferedImage {

    public FSImage() {
        // empty constructor
    }
}

However, I get some errors:

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)
...

What am I doing wrong?


The "empty constructor" implicitly calls the nullary base class constructor, and no such constructor exists in BufferedImage. You need to explicitly call the appropriate base class constructor:

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


When extending a class, the subclass must eventually call some constructor of the superclass (whether directly or chaining through other constructors it defines which ultimately call a constructor of the superclass). How you obtain the parameters is often done by implementing a constructor with the same parameters then passing them on with super.

BufferedImage(int width, int height, int imageType)

Is one of the constructors to BufferedImage(). Since you are extending it, you could supply this constructor.

FSImage(int width, int height, int imageType) 

Then call super() which calls the constructor of the superclass:

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

However it should be noted that as long as you call a valid super() constructor, your own constructor need not have the same signature. For example, the following would be a legitimate constructor:

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

If you do not define any constructors the compiler will by default call the no-argument, default constructor of the superclass. Since in this case, it does not exist you must call an existing constructor.


There is no default constructor defined in BufferedImage so you cannot do

new BufferedImage();

in a similar fashion, if you create a subclass for BufferedImage, it cannot interface with it's super class without satisfying the initialization requirements. So, the sub class constructor must call atleast one of the super class constructor.

you can try this..

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

}

or

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

}


BufferedImage doesn't have an empty constructor. When extending it, your derived class will need to call a specific constructor in its super class (BufferedImage).


Because BufferedImage has not any no argument constructor.


Looks like the class you're trying to extend has some required constructor parameters. This similar question has the syntax you will need to use in order to extend the class:

Java extending class with the constructor of main class has parameter


BufferedImage doesn't have a no arg constructor you need to have a super() call inside the constructor with corresponding arguments (that require the size of the image and the type of how it's stored internally)


You must implement one or more of the existing constructors for the class you are extending

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜