138 - 打球过程

138 - 打球过程

Time Limit: 1000   Memory Limit: 65535
Submit: 424  Solved: 276

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.*;
abstract class BallMatch
{
	 public final void compete(String res){
	        checkin();
	        start();
	        play();
	        end();
	        annouceResult(res);
	    }
	public void checkin()
	{
		System.out.println("now checking in");
	};
	public void start()
	{
		System.out.println("now starting");
	}
	public abstract void play();
	public void end()
	{
		System.out.println("now ending");
	}
	public void annouceResult(String result) 
	{
		System.out.println("now annoucing result: "+result);
	}
	
}
class FootballMatch extends BallMatch
{
	String result;
	FootballMatch(String res)
	{
        this.result = res;
    }
    public void play()
    {
        System.out.println("now playing football");
    }
}
class BasketBallMatch extends BallMatch
{
	String result;
    BasketBallMatch(String res)
    {
        this.result = res;
    }
	public void play()
	{
		System.out.println("now playing basketball");
	}
}
public class Main
{
	public static void main(String[] args)
	{
		Scanner scan = new Scanner(System.in);
		int i;
		i = scan.nextInt();
		String score = scan.next();
		BallMatch match;
		if(i == 1)
		{
			match = new FootballMatch(score);
			match.compete(score);
		}
		else if(i == 2)
		{
			match = new BasketBallMatch(score);
			match.compete(score);
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值