已有类Car、Plane。请创建接口Weapon以及类Tank和Fighter。
接口Weapon中定义了无参数和返回值的方法shoot。
类Tank和Fighter分别继承Car和Plane,并且都实现了接口weapon。
请在类Tank和Fighter中分别实现接口weapon中的方法Shoot,Tank类中的shoot方法能够打印“发射大炮”,Fighter类中的shoot方法能够打印“发射火箭”。
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如: class Car{ public void move() { System.out.println("running"); } } class Plane{ public void move() { System.out.println("flying"); } } /* 请在这里填写答案 */ public class Main{ public static void main(String argv[]){ Tank tank = new Tank(); Fighter fighter = new Fighter(); tank.move(); tank.shoot(); fighter.move(); fighter.shoot(); Weapon tank2 = new Tank(); Weapon fighter2= new Fighter(); tank2.shoot(); fighter2.shoot(); } }
输出样例:无输入
输出样例:
在这里给出相应的输出。例如:
running
发射大炮
flying
发射火箭
发射大炮
发射火箭
答案:
// 定义接口Weapon
interface Weapon {
void shoot();
}
// 定义类Tank,继承自Car,并实现接口Weapon
class Tank extends Car implements Weapon {
@Override
public void shoot() {
System.out.println("发射大炮");
}
}
// 定义类Fighter,继承自Plane,并实现接口Weapon
class Fighter extends Plane implements Weapon {
@Override
public void shoot() {
System.out.println("发射火箭");
}
}