2021-10-30

这篇文章介绍了如何为Monopoly、Chess和Battleships三种经典桌游创建抽象Game类,并在子类中实现getName和play方法。通过代码实例,展示了如何根据不同游戏规则打印相应的指令。
摘要由CSDN通过智能技术生成

A board game company creates new board games every year. While all the games have different rules, they also are all similar in that they each have a name and a play() method.
We need to create 3 different games - Monopoly, Chess and Battleships. In the play() method Monopoly should print “Buy all property.”, Battleships - “Sink all ships.”, and Chess - “Kill the enemy king.”
Complete the code by implementing the getName() and play() methods inherited from abstract Game class.

class Main {
   public static void main(String[] args) {
       //do not touch this code
       Monopoly monopoly = new Monopoly();
       Chess chess = new Chess();
       Battleships battleships = new Battleships();
       
       monopoly.play();
       chess.play();
       battleships.play();
   }
}

abstract class Game {
   abstract String getName();
   abstract void play();
}

class Monopoly extends Game {

   //give "Monopoly" name to game
   String getName() {
        String a="Monopoly";
       return a;
   }

   // play method should print "Buy all property."
   void play() {
       System.out.println("Buy all property.");
   }
}

class Chess extends Game {

   //give "Chess" name to game
   String getName() {
   String b="Chess";
       return b;
   }

   // play method should print "Kill the enemy king"
   void play() {
       System.out.println("Kill the enemy king.");
   }
}

class Battleships extends Game {

   //give "Battleships" name to game
   String getName() {
   String c="Battleships";
       return c;
   }

   // play method should print "Sink all ships"
   void play() {
       System.out.println("Sink all ships.");
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

melanie萱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值