Java - 无法扩展类?
我正在尝试使用此代码扩展 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
“空构造函数”隐式调用空基类构造函数,并且 BufferedImage 中不存在这样的构造函数。您需要显式调用适当的基类构造函数:
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:当扩展一个类时,子类最终必须调用超类的某个构造函数(无论是直接调用还是通过它定义的其他构造函数链接,最终调用超类的构造函数)。获取参数的方式通常是通过实现具有相同参数的构造函数,然后使用 super 传递它们来完成。
是 BufferedImage() 的构造函数之一。由于您正在扩展它,因此您可以提供此构造函数。
然后调用 super() ,它调用超类的构造函数:
但是应该注意的是,只要您调用有效的 super() 构造函数,您自己的构造函数就不需要具有相同的签名。例如,以下内容将是合法的构造函数:
如果未定义任何构造函数,则编译器将默认调用超类的无参数默认构造函数。由于在这种情况下,它不存在,因此您必须调用现有的构造函数。
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.
Is one of the constructors to BufferedImage(). Since you are extending it, you could supply this constructor.
Then call super() which calls the constructor of the superclass:
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:
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.
BufferedImage 中没有定义默认构造函数,因此您不能
以类似的方式执行操作,如果您为 BufferedImage 创建子类,则在不满足初始化的情况下,它无法与其超类交互要求。因此,子类构造函数必须至少调用超类构造函数之一。
你可以尝试这个..
或者
There is no default constructor defined in
BufferedImage
so you cannot doin 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..
or
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
).因为 BufferedImage 没有任何无参数构造函数。
Because
BufferedImage
has not any no argument constructor.看起来您尝试扩展的类有一些必需的构造函数参数。这个类似的问题具有扩展类所需的语法:
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
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)
您必须为要扩展的类实现一个或多个现有构造函数
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