&引用;无法从静态上下文引用非静态方法";-JAVA
对于一些java作业,我们被告知创建一个制作拼贴的程序,其中一种方法是拼贴的“碎片”(图像)之一将按宽度和高度缩放。 预先编写的类“fragment”已经有了缩放图像的方法:&引用;无法从静态上下文引用非静态方法";-JAVA,java,bluej,Java,Bluej,对于一些java作业,我们被告知创建一个制作拼贴的程序,其中一种方法是拼贴的“碎片”(图像)之一将按宽度和高度缩放。 预先编写的类“fragment”已经有了缩放图像的方法: public void scaleFragment(int newWidth, int newHeight) { width = newWidth; height = newHeight; } 因此,在主类中,我通过以下操作调用此方法: public void makeTiles(int tileWidt
public void scaleFragment(int newWidth, int newHeight)
{
width = newWidth;
height = newHeight;
}
因此,在主类中,我通过以下操作调用此方法:
public void makeTiles(int tileWidth, int tileHeight)
{
for (Fragment sm:collage){
Fragment.scaleFragment(tileWidth, tileHeight);
}
}
然而,当我尝试运行此程序时,我出现了以下错误:无法从静态上下文引用非静态方法scaleFragment(int,int) 在阅读本网站后,我发现您需要包装该类的一个实例?因此我做了以下操作:
Fragment fragment = new Fragment();
for (Fragment sm:collage){
Fragment.scaleFragment(tileWidth, tileHeight);
}
然而,这会产生错误“类片段中的构造函数错误无法应用于给定类型”
主类替换了arraylist拼贴,如下所示:
Fragment fragment = new Fragment();
for (Fragment sm:collage){
Fragment.scaleFragment(tileWidth, tileHeight);
}
有人能帮我解决这个问题吗?提前感谢。问题是,您不能使用类名作为限定符调用
scaleFragment
(如Fragment.scaleFragment(…)
)-这只适用于静态方法。请尝试以下方法:
public void makeTiles(int tileWidth, int tileHeight)
{
for (Fragment sm:collage){
sm.scaleFragment(tileWidth, tileHeight);
}
}
这将为拼贴的每个元素调用scaleFragment
。(此建议假设,collage
是一个已填充了Fragment
实例的集合或数组。您不显示什么是collage
,因此如果假设错误,您必须进行相应调整。)