参照如下UML图实现英雄(IHero)接口、刺客(Assassin)类、战士(Warrior)类、游戏(Game)类,并在Main类中创建测试对象,输出正确的结果。 注意:1、Warrior,Assassin类的name属性是私有的,每个类都有一个有参数的构造函数;2、注意,IHero中的combo()函数是default方法;3、Game类中包含一个IHero的数组,在Game类的play方法中输出信息;4、在main方法中创建IHero对象数组,并通过此数组创建Game对象,然后调用Game对象的play方法。 输入说明: 无 输出说明: *****我是阿珂*****我的一技能是:隐身...我的二技能是:砍掉敌人一半血...我的连招是:隐身...砍掉敌人一半血...撤退...*****我是程咬金*****我的一技能是:加防御...我的二技能是:砍两下...我的连招是:加防御...砍两下...回血...
时间: 2023-07-24 21:40:11 浏览: 50
代码如下:
```java
interface IHero{
void combo();
}
class Assassin implements IHero{
private String name;
public Assassin(String name){
this.name = name;
}
public void combo(){
System.out.println("*****我是" + name + "*****");
System.out.println("我的一技能是:隐身...");
System.out.println("我的二技能是:砍掉敌人一半血...");
System.out.println("我的连招是:隐身...砍掉敌人一半血...撤退...");
}
}
class Warrior implements IHero{
private String name;
public Warrior(String name){
this.name = name;
}
public void combo(){
System.out.println("*****我是" + name + "*****");
System.out.println("我的一技能是:加防御...");
System.out.println("我的二技能是:砍两下...");
System.out.println("我的连招是:加防御...砍两下...回血...");
}
}
class Game{
private IHero[] heroes;
public Game(IHero[] heroes){
this.heroes = heroes;
}
public void play(){
for(IHero hero : heroes){
hero.combo();
}
}
}
public class Main{
public static void main(String[] args){
IHero[] heroes = {
new Assassin("阿珂"),
new Warrior("程咬金")
};
Game game = new Game(heroes);
game.play();
}
}
```
输出结果为:
```
*****我是阿珂*****
我的一技能是:隐身...
我的二技能是:砍掉敌人一半血...
我的连招是:隐身...砍掉敌人一半血...撤退...
*****我是程咬金*****
我的一技能是:加防御...
我的二技能是:砍两下...
我的连招是:加防御...砍两下...回血...
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)