Java——打球过程

Description

利用模板方法来构造相关类实现下述过程:
各种球类的玩法虽然不同,但是球类比赛的过程是类似的,都包含如下几个步骤:
1球员报道-->2比赛开始-->3比赛-->4比赛结束-->5公布比赛成绩,且其中1 2 4步相同 第3步根据球类不同,玩法不同,第5步根据得分不同,公布方式结果不同
构造类BallMatch表示球类比赛,包含方法compete表示真个比赛过程
构造各个比赛过程的函数checkin,start,play,end,annouceResult
打印信息如下:
now checking in
now starting
now playing football
now ending
now annoucing result: 2-3
构造类FootballMatch和BasketBallMatch,实现具体的比赛过程。

在main函数中,读入整数i,如果为1,则构造一个足球比赛过程,如果为2则构造一个篮球比赛过程
打印比赛过程

Input

比赛类型 比分

Output

比赛过程信息

Sample Input

1 2-3

Sample Output

now checking in
now starting
now playing football
now ending
now annoucing result: 2-3
import java.util.Scanner;

public class Main{

    public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);

       int num = scan.nextInt();

       String point = scan.next();

       if(num == 1)

       {

           BallMatch  ball = new FootballMatch(point);

           ball.checkin();

           ball.start();

           ball.play();

           ball.end();

           ball.annouceResult(point);

       }

       else

       {

           BallMatch  ball = new BasketBallMatch(point);

           ball.checkin();

           ball.start();

           ball.play();

           ball.end();

           ball.annouceResult(point);

       }

         

         

         

    }

}

abstract class BallMatch{

    private String points;

    public BallMatch(String points)

    {

        this.points = points;

    }

    public String getPoints()

    {

        return points;

    }

    public void checkin()

    {

        System.out.println("now checking in");

    }

    public void start()

    {

        System.out.println("now starting");

    }

    abstract public void play();

    public void end()

    {

        System.out.println("now ending");

    }

    abstract public void annouceResult(String points);

}

class FootballMatch extends  BallMatch{

    public FootballMatch(String points)

    {

        super(points);

    }

    public void play()

    {

        System.out.println("now playing football");

    }

    public void annouceResult(String points) 

    {

        System.out.println("now annoucing result: "+super.getPoints());

    }

    

}

class BasketBallMatch extends BallMatch{

    public BasketBallMatch(String points)

    {

        super(points);

    }

    public void play()

    {

        System.out.println("now playing basketball");

    }

    public void annouceResult(String points) 

    {

        System.out.println("now annoucing result: "+super.getPoints());

    }

}

 

你可能感兴趣的:(Java)