gpt4 book ai didi

java - 非静态变量pRefix不能从静态上下文中引用

转载 作者:行者123 更新时间:2023-12-02 11:09:31 24 4
gpt4 key购买 nike

我正在尝试使用setFavoritePicture (Picture pRef)方法设置图片。
该方法应该设置一个在主方法中要调用的收藏图片,但是我一直收到表示nonstatic variable pRef cannot be referenced from a static context的编译器错误。我对Java还是比较陌生,因此非常感谢您能提供的任何帮助

public class House
{
String owner;
Picture pRef;
Picture [] picArray;
Picture favPic;

public void showArtCollection ()
{

ArtWall aWall = new ArtWall(600,600);
aWall.copyPictureIntoWhere(favPic,250,100);
aWall.copyPictureIntoWhere(picArray[0],51,330);
aWall.copyPictureIntoWhere(picArray[1],151,330);
aWall.copyPictureIntoWhere(picArray[2],351,280);
aWall.show();

}



public House (String param)
{

this.owner = param;
this.picArray = new Picture [3];
this.favPic = new Picture (FileChooser.pickAFile ());
this.picArray [0] = new Picture (FileChooser.pickAFile ());
this.picArray [1] = new Picture (FileChooser.pickAFile ());
this.picArray [2] = new Picture (FileChooser.pickAFile ());




}

public void setFavoritePicture (Picture pRef)
{
pRef = favPic;
}

public void setOneOtherPicture (int which,Picture pRef)
{

}


public void swapGivenOtherWithFavorite (int which)
{
Picture tempSaver;
tempSaver = pRef;
pRef = picArray [which];
picArray [which] = tempSaver;
}


public void addPicture (Picture pictureAdded)
{
pRef = pictureAdded;


}

public void showPicture ()
{

picArray [0].explore ();
picArray [1].explore ();
picArray [2].explore ();
favPic.explore ();


}


public static void main (String [] args)
{
House PhDsHouse = new House ("Mad PH.D.");
PhDsHouse.setFavoritePicture (pRef);
PhDsHouse.swapGivenOtherWithFavorite (2);
PhDsHouse.showArtCollection ();


}

}

最佳答案

我看到的错误如下:
PhDsHouse.setFavoritePicture (pRef);pRef中定义了main的位置?因此,您在该语句处出错。

我猜,您想创建新的Picture对象,然后使用PhDsHouse将其分配给setFavoritePicture。这是真的?如果是,则需要在Picture pRef = new Picture();之前执行类似setFavoritePicture的操作……那么您应该很好。

另外,以下功能对我来说非常可疑

public void setFavoritePicture (Picture pRef)
{
pRef = favPic;
}

应该是
 public void setFavoritePicture (Picture  favPic)
{
pRef = favPic;
}

因为,我看不到在您的代码中 favPic的定义/初始化位置...。否则,当您访问 NULL pointer exceptions时,您将得到 pRef,因为 favPicNULL,它将被分配给 pRef

关于java - 非静态变量pRefix不能从静态上下文中引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16377626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com