Java - 无法扩展类?

发布于 2024-11-06 03:34:20 字数 572 浏览 6 评论 0原文

我正在尝试使用此代码扩展 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)
...

我做错了什么?

I'm trying to extend java.awt.image.BufferedImage using 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

自此以后,行同陌路 2024-11-13 03:34:20

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

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

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
}
樱娆 2024-11-13 03:34:20

当扩展一个类时,子类最终必须调用超类的某个构造函数(无论是直接调用还是通过它定义的其他构造函数链接,最终调用超类的构造函数)。获取参数的方式通常是通过实现具有相同参数的构造函数,然后使用 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 );
}

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

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.

无戏配角 2024-11-13 03:34:20

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

}

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

}
如此安好 2024-11-13 03:34:20

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

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

潦草背影 2024-11-13 03:34:20

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

Because BufferedImage has not any no argument constructor.

美人如玉 2024-11-13 03:34:20

看起来您尝试扩展的类有一些必需的构造函数参数。这个类似的问题具有扩展类所需的语法:

Java扩展类,主类的构造函数有参数

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

独享拥抱 2024-11-13 03:34:20

BufferedImage 没有无参数构造函数,您需要在构造函数内使用相应的参数进行 super() 调用(需要图像的大小及其内部存储的类型)

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)

回梦 2024-11-13 03:34:20

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文