这是我正在为练习而制作的小行星改造中的一艘船的子类代码。
package comets;
public class Ship extends SpaceObject{
protected double angle;
protected double speed;
public Ship(double xPos,double yPos,double xVel,double yVel){
super(xPos, yPos, xVel, yVel, 10);
}
public void accelerate(){
this.xVelocity+=.1*Math.sin(angle);
this.yVelocity+=.1*Math.cos(angle);
}
public double getSpeed(){
return speed=Math.sqrt(Math.pow(this.xVelocity, 2)+Math.pow(this.yVelocity,2));
}
public Shot fire(){
//need to return a shot
return null;
}
public double getAngle(){
return angle;
}
public void rotateLeft(){
angle+=.1;
}
public void rotateRight(){
angle-=.1;
}
}
如您所见,我不确定如何返回公共 Shot fire() 方法。我目前让它返回 null 但我需要它返回 Shot 以便游戏中的船可以射击子弹。Bellow 是一个镜头类,我在其中定义了镜头的构造函数。
package comets;
public class Shot extends SpaceObject{
protected int counter=0;
public Shot(double xPos,double yPos,double xVel,double yVel){
super(xPos, yPos, xVel, yVel, 3);
}
public int getAge(){
return counter;
}
public void move(){
counter++;
super.move();
}
}
任何帮助将不胜感激,谢谢。