Java-无法扩展类?
我试图使用以下代码扩展Java-无法扩展类?,java,bufferedimage,Java,Bufferedimage,我试图使用以下代码扩展java.awt.image.buffereImage: import java.awt.image.BufferedImage; public class FSImage extends BufferedImage { public FSImage() { // empty constructor } } 但是,我发现了一些错误: no suitable constructor found for BufferedImage() co
java.awt.image.buffereImage
:
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)
...
找不到适合BuffereImage()的构造函数
构造函数java.awt.image.BuffereImage.BuffereImage(java.awt.image.ColorModel,java.awt.image.WritableRaster,boolean,java.util.Hashtable)不适用(实际参数列表和形式参数列表长度不同)
...
我做错了什么?该“空构造函数”隐式调用空基类构造函数,而BuffereImage
中不存在此类构造函数。您需要显式调用相应的基类构造函数:
public FSImage() {
super(args); // replace "args" with actual arguments to BufferedImage()
// other stuff
}
FSImage() {
super( 100, 100, TYPE_INT_RGB );
}
“空构造函数”隐式调用空基类构造函数,buffereImage
中不存在此类构造函数。您需要显式调用相应的基类构造函数:
public FSImage() {
super(args); // replace "args" with actual arguments to BufferedImage()
// other stuff
}
FSImage() {
super( 100, 100, TYPE_INT_RGB );
}
buffereImage
没有空构造函数。扩展它时,派生类将需要调用其超类中的特定构造函数(BufferedImage
)。BufferedImage
没有空构造函数。扩展它时,派生类将需要调用其超类中的特定构造函数(buffereImage
).因为BuffereImage
没有任何无参数构造函数。因为BuffereImage
没有任何无参数构造函数。必须为要扩展的类实现一个或多个现有构造函数
FSImage(int width, int height, int imageType)
必须为要扩展的类实现一个或多个现有构造函数
在
buffereImage
中没有定义默认构造函数,因此无法执行此操作
new BufferedImage();
以类似的方式,如果为buffereImage
创建子类,则在不满足初始化要求的情况下,它无法与其超类进行接口。因此,子类构造函数必须调用至少一个超类构造函数
FSImage(int width, int height, int imageType)
你可以试试这个
public FSImage(int arg0, int arg1, int arg2) {
super(arg0, arg1, arg2);
}
或
buffereImage
中没有定义默认构造函数,因此无法执行此操作
new BufferedImage();
以类似的方式,如果为buffereImage
创建子类,则在不满足初始化要求的情况下,它无法与其超类进行接口。因此,子类构造函数必须调用至少一个超类构造函数
FSImage(int width, int height, int imageType)
你可以试试这个
public FSImage(int arg0, int arg1, int arg2) {
super(arg0, arg1, arg2);
}
或
扩展类时,子类最终必须调用超类的某个构造函数(无论是直接调用还是通过它定义的其他构造函数链接,后者最终调用超类的构造函数)。获取参数的方法通常是使用相同的参数实现构造函数,然后使用super传递它们
BufferedImage(int width, int height, int imageType)
是BuffereImage()的构造函数之一。因为您正在扩展它,所以可以提供这个构造函数
FSImage(int width, int height, int imageType)
然后调用super(),它调用超类的构造函数:
FSImage(int width, int height, int imageType) {
super( width, height, imageType );
}
但是应该注意的是,只要调用有效的super()构造函数,您自己的构造函数就不需要具有相同的签名。例如,以下内容将是合法的构造函数:
public FSImage() {
super(args); // replace "args" with actual arguments to BufferedImage()
// other stuff
}
FSImage() {
super( 100, 100, TYPE_INT_RGB );
}
如果未定义任何构造函数,编译器将在默认情况下调用no参数,即超类的默认构造函数。因为在这种情况下,它不存在,所以必须调用现有的构造函数。扩展类时,子类最终必须调用超类的某个构造函数(无论是直接调用还是通过它定义的其他构造函数链接,这些构造函数最终调用超类的构造函数)。获取参数的方法通常是使用相同的参数实现构造函数,然后使用super传递它们
BufferedImage(int width, int height, int imageType)
FSImage(int width, int height, int imageType)
是BuffereImage()的构造函数之一。因为您正在扩展它,所以可以提供这个构造函数
FSImage(int width, int height, int imageType)
然后调用super(),它调用超类的构造函数:
FSImage(int width, int height, int imageType) {
super( width, height, imageType );
}
但是应该注意的是,只要调用有效的super()构造函数,您自己的构造函数就不需要具有相同的签名。例如,以下内容将是合法的构造函数:
public FSImage() {
super(args); // replace "args" with actual arguments to BufferedImage()
// other stuff
}
FSImage() {
super( 100, 100, TYPE_INT_RGB );
}
如果未定义任何构造函数,编译器将在默认情况下调用no参数,即超类的默认构造函数。因为在这种情况下,它不存在,所以您必须调用现有的构造函数。看起来您试图扩展的类有一些必需的构造函数参数。此类似问题具有扩展类所需的语法:
FSImage(int width, int height, int imageType)
看起来您试图扩展的类有一些必需的构造函数参数。此类似问题具有扩展类所需的语法:
BuffereImage没有无参数构造函数您需要在构造函数中有一个带相应参数的super()调用(需要图像的大小和内部存储的类型)BuffereImage没有无参数构造函数您需要有一个super()使用相应的参数在构造函数内部调用(需要图像的大小及其内部存储方式的类型)否,您根本不重写构造函数。。。但是,您提供的每个构造函数都必须链接到超类中的构造函数或派生类中的另一个构造函数。(如果您不提供任何构造函数,那么必须为编译器提供一个可访问的无参数构造函数来链接到。)不,您根本不重写构造函数。。。但是,您提供的每个构造函数都必须链接到超类中的构造函数或派生类中的另一个构造函数。(如果您不提供任何构造函数,那么必须为编译器提供一个可访问的无参数构造函数以链接到。)“扩展类时,还必须实现超类所具有的构造函数。”这显然是错误的。测试一下,是的,你是对的。正确地说,子类最终必须调用某些co