我正在尝试操作抽象类中的公共(public)方法。 我尝试从扩展另一个抽象类的公共(public)类内部的公共(public)方法内部操作该方法,但编译器给了我:
"non-static method method_name() cannot be referenced from a static context"
我需要什么才能使其运行该方法?
public class Dingo extends Animal
{
public void act()
{
if (kangarooCrossing())
{
Weapon.killAnimalMySquare(); //<<<<<<<<<< THE Problematic line
}
if(canMove())
move();
else
changeDirection();
}
// returns true if a Kangaroo is crossing.
private boolean kangarooCrossing()
{
Actor kangaroo = getOneObjectAtOffset(0, 0, Kangaroo.class);
if(kangaroo != null) {
return true;
}
else {
return false;
}
}
}
abstract class Weapon extends Actor
{
/**Kills an animal that steps on current square*/
public void killAnimalMySquare()
{
Actor animal = getOneObjectAtOffset(0, 0, Animal.class);
if(animal != null)
getWorld().removeObject(animal);
}
/**returns true if an animal is crossing*/
public boolean animalCrossing()
{
Actor animal = getOneObjectAtOffset(0, 0, Animal.class);
if(animal != null)
return true;
return false;
}
}
谢谢!
最佳答案
看来您调用目标方法的方法是静态的。
您需要创建此类的实例并在实例上调用此方法。
关于java - 操作方法时出现问题 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6103953/