Possible Duplicate:
What is the reason behind 「non-static method cannot be referenced from a static context」?非靜態方法不能從靜態上下文中引用?
我有以下方法:
public void fillMachine(Game game)
{
// Colours of balls are evenly spread between these colours,
// in ascending order.
Color [] colourGroupColours
= new Color [] {Color.red, Color.orange, Color.yellow,
Color.green, Color.blue, Color.pink,
Color.magenta};
// This happiness change will show up when the GUI is added.
Color ballColour;
int noOfBalls = game.getMachineSize();
for (int count = 1; count <= noOfBalls; count++)
{
// The colour group is a number from 0
// to the number of colour groups - 1.
// For the nth ball, we take the fraction
// (n - 1) divided by the number of balls
// and multiply that by the number of groups.
int colourGroup = (int) ((count - 1.0)/(double) noOfBalls
* (double) colourGroupColours.length);
ballColour = colourGroupColours[colourGroup];
game.machineAddBall(makeNewBall(count, ballColour));
} // for
} // fillMachine
在主類我有fillMachine(game1);
我收到錯誤:non-static method fillMachine(Game) cannot be referenced from a static context fillMachine(game1);
我不知道如何解決這雖然。
這裏有沒有問題? – ewok 2012-02-23 19:44:03
將static添加到您的方法 – VirtualTroll 2012-02-23 19:45:21