用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

打球过程-模板方法

2018-09-29 作者:云代码会员举报

[java]代码库

import java.util.Scanner; 
public class Main{ 
    public static void main(String[] args) { 
         Scanner scan = new Scanner(System.in); 
         int n = scan.nextInt();
         String s = scan.next();
         if(n == 1) {
        	 BallMatch match = new FootballMatch(s);
        	 match.compete();
         }
         else if(n == 2) {
        	 BallMatch match = new BasketballMatch(s);
        	 match.compete();
         }

         scan.close();  
    } 
}  

abstract class BallMatch{
	private String s;
	public BallMatch(String _s) {
		s = _s;
	}
	public String getS() {
		return s;
	}
	public final void compete() {
		checkin();
		start();
		play();
		end();
		annouceResult();
	}
	private void checkin() {
		System.out.println("now checking in");
	}
	private void start() {
		System.out.println("now starting");
	}
	public abstract void play();
	private void end() {
		System.out.println("now ending");
	}
	public abstract void annouceResult();
}

class FootballMatch extends BallMatch{
	public FootballMatch(String _s) {
		super(_s);
	}
	public void play() {
		System.out.println("now playing football");
	}
	public void annouceResult() {
		System.out.println("now annoucing result: "+ super.getS());
	}
}

class BasketballMatch extends BallMatch{
	public BasketballMatch(String _s) {
		super(_s);
	}
	public void play() {
		System.out.println("now playing basketball");
	}
	public void annouceResult() {
		System.out.print("now annoucing result: "+ super.getS());
	}
}


分享到:
更多

网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。