Java:超类不会覆盖功能

问题描述:

我一直在做一个游戏一段时间,我想为每种类型的生物都有不同的类。现在,所有不同的生物的AI都以长开关运行,并且我希望一个超类能够用那个AI的那个生物的功能来超越这个功能。我有这个设置,但不会覆盖。Java:超类不会覆盖功能

我忘了什么吗?

Bunny.java:

package creature; 

import org.newdawn.slick.opengl.Texture; 

import creature.Creature; 
import creature.CreatureType; 
import data.Tile; 

public class Bunny extends Creature{ 

    public Bunny(CreatureType type, float x, float y, float speed1) { 
     super(type, x, y, speed1); 

    } 

    public void AI(int type) { 
     System.out.println("test"); 
    } 

} 

Creature.java:

public Creature(CreatureType type, float x, float y, float speed1) { 
    this.texture = drawImg(type.textureName); 
    this.textureHamster = drawImg("creatures/HamsterFace"); 
    this.healthBackground = drawImg("health_background"); 
    this.healthForeground = drawImg("health_foreground"); 
    this.healthBorder = drawImg("health_border"); 
    this.startTile = startTile; 
    this.x = x; 
    this.y = y; 
    this.intX = (int) x; 
    this.intY = (int) y; 
    this.width = texture.getImageWidth(); 
    this.height = texture.getImageHeight(); 
    this.speed1 = speed1; 
    this.speed = speed; 
    this.intspeed = speed; 
    this.grid = grid; 
    this.health = type.health; 
    this.inithealth = type.health; 
    this.hiddenHealth = health; 
    this.startHealth = health; 
    this.dir = false; 
    this.dchosen = false; 
    this.setx = 0; 
    this.hurt = 0; 
    this.panick = 0; 
    this.deathWish = 0; 
    this.pdir = -1; 
    this.myX = x; 
    this.myY = HEIGHT/2; 
    this.right = false; 
    this.left = false; 
    this.fade = 0; 
    this.fir = true; 
    this.aiType = type.aiType; 
    this.yOffset = 0; 
} 

..... 

public void AI(int type) { 
    if(panic > 0) 
     panic--; 
    hurt(); 
    speed = speed1; 
    switch(type) { 

    case 1: 
     if(panic > 0) { 
      if(pickRandom(150, 300) < 10) { 
       direction = !direction; 
      } 

      if(direction) { 
       if(!right) { 
        x += speed; 
       } else { 
        if(falling < 2) 
        gravity = 8; 
       } 
      } else { 
       if(!left) { 
        x -= speed; 
       } else { 
        if(falling < 2) 
        gravity = 8; 
       } 
      } 

     } else { 
      if(getRange(WIDTH/2, myX) > 200) { 
       directionCoolDown++; 
       if(directionCoolDown > pickRandom(150, 3000)) { 
        direction = !direction; 
        directionCoolDown = 0; 
       } 



       if(direction) { 
        if(!right) { 
         x += speed/3.2; 
        } else { 
         if(falling < 2) 
         gravity = 8; 
        } 
       } else { 
        if(!left) { 
         x -= speed/3.2; 
        } else { 
         if(falling < 2) 
         gravity = 8; 
        } 
       } 


      } else { 
       if(myX < WIDTH/2) { 
        direction = true; 
       } else { 
        direction = false; 
       } 




      } 
     } 
     break; 

    case 2: 

     yOffset = -25; 
     if(!angry) { 
      pdir = 0; 
      if(getRange(Player.getX(), myX) < 300) { 
       hamsterFace = true; 
      } else { 
       hamsterFace = false; 
      } 

      if(!hamsterFace) { 
       directionCoolDown++; 
       if(directionCoolDown > pickRandom(150, 3000)) { 
        direction = !direction; 
        directionCoolDown = 0; 
       } 

       if(direction) { 
        if(!right) { 
         x += speed/3.2; 
        } else { 
         if(falling < 2) 
         gravity = 8; 
        } 
       } else { 
        if(!left) { 
         x -= speed/3.2; 
        } else { 
         if(falling < 2) 
         gravity = 8; 
        } 
       } 
      } 
     } else { 
      pdir++; 
      hamsterFace = false; 
      if(myX < Player.getX()) { 
       direction = true; 
      } else { 
       direction = false; 
      } 

      if(direction) { 
       if(!right) { 
        x += speed/1; 
       } else { 
        if(falling < 2) 
        gravity = 8; 
       } 
      } else { 
       if(!left) { 
        x -= speed/1; 
       } else { 
        if(falling < 2) 
        gravity = 8; 
       } 
      } 

      if(getRange(myX, Player.getX()) < 5 && getRange(myY, Player.getY()) < 5) { 
       hurtPlayer(-2); 
       direction = !direction; 
       if(direction) { 
        if(!right) { 
         x += speed * 10; 
        } else { 
         if(falling < 2) 
         gravity = 8; 
        } 
       } else { 
        if(!left) { 
         x -= speed * 10; 
        } else { 
         if(falling < 2) 
         gravity = 8; 
        } 
       } 
      } 
     } 

     if(panic > 1) { 
      angry = true; 
     } else { 
      if(pdir > pickRandom(1000,2000)) { 
       angry = false; 
      } 
     } 

     break; 

    } 
} 

..... 

(这两个类是在相同的封装)

编辑:我固定错字....

+0

我在'Creature'类中看不到有签名'void AI()'的方法,所以这里没有什么可以重写。 –

+0

此外,我没有看到在'Bunny'类中签名'void AI(int)'的方法,所以它不能覆盖任何东西。 –

+0

将'@ Override'添加到您正在覆盖的方法中。你的IDE会告诉你什么是错的 – n247s

你有兔子类:

public void AI() { 
    System.out.println("test"); 
} 
在生物类

public void AI(int type) { 
    if(panic > 0) 
    .... 

所以

void AI(int type)void AI()同样的方法(检查签名,以及他们如何采取不同的参数!)

因此兔子班并不压倒父母班的任何东西

- 编辑:

现在你的类有一个方法void AI(int type)那么我们可以说, 兔子覆盖怪物AI方法和每次你打电话bunny.AI(f)你的兔子方法将被调用!

+0

这仍然不起作用...在放置重写注释后,我没有得到任何错误。我觉得它没有被称为...我需要打电话吗? – Ekrcoaster

+0

但是你改变了方法签名为public void AI(int type)?如果重写给出错误,则找不到父方法。 – wumpz