6-9 坦克与火箭

已有类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("发射火箭");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值