PTA Java程序设计
7-4 打球过程
懒人一键copy
1.利用模板方法来构造相关类实现下述过程:
1、球员报道
2、比赛开始
3、比赛
4、比赛结束
5、公布比赛成绩
其中1 2 4步相同 第3步根据球类不同,玩法不同,第5步根据得分不同,公布方式结果不同
构造类BallMatch
表示球类比赛,包含方法compete
表示整个比赛过程
分析:
- 构建类
BallMatch
,在类中包含公共方法compete()
- 创建球类比赛共有的方法
checkin()
、start()
、end()
//父类球类比赛
class BallMatch{
//调用公共的方法
public void compete(){
checkin();
start();
end();
}
//第一步 球员报道
public void checkin(){
System.out.println("now checking in");
}
//第二步 比赛开始
public void start(){
System.out.println("now starting");
}
// 第四步 比赛结束
public void end(){
System.out.println("now ending");
}
}
2.构造类FootballMatch实现具体的比赛过程。
分析:
- 定义比赛分数
score
,生成有参构造函数- 定义足球比赛特有的方法:
play()
、annouceResult()
- 重写父类球类比赛
compete()
方法,在方法中增加调用足球比赛特有的方法
//足球比赛继承父类球类比赛
class FootballMatch extends BallMatch{
String score;
public FootballMatch(String score) {
this.score = score;
}
//定义公共的方法
@Override
public void compete() {
checkin();
start();
play();
end();
annouceResult();
}
//足球比赛
public void play(){
System.out.println("now playing football");
}
//足球比赛得分
public void annouceResult(){
System.out.println("now annoucing result: "+score);
}
}
3.构造类BasketBallMatch实现具体的比赛过程。
分析:
- 定义比赛分数
score
,生成有参构造函数- 定义篮球比赛特有的方法:
play()
、annouceResult()
- 重写父类球类比赛
compete()
方法,在方法中增加调用篮球比赛特有的方法- d
//篮球比赛继承父类球类比赛
class BasketBallMatch extends BallMatch{
String score;
public BasketBallMatch(String score) {
this.score = score;
}
//重写父类的公共方法并增加子类独有的play()和annouceResult()方法
@Override
public void compete() {
checkin();
start();
play();
end();
annouceResult();
}
//篮球比赛
public void play(){
System.out.println("now playing basketball");
}
//篮球比赛得分
public void annouceResult(){
System.out.println("now annoucing result: "+score);
}
}
4.main函数运行
在main函数中,读入整数i,如果为1,则构造一个足球比赛过程,如果为2则构造一个篮球比赛过程
打印比赛过程
分析:
- 创建
Scanner
对象来获取整数i
- 用if函数判断输入的值是否满足条件
- 如果为1则创建
FootballMatch
对象,并调用compete()
方法(子类足球比赛已经重写父类方法)- 如果为2则创建
FootballMatch
对象,则调用compete()
方法(子类篮球比赛已经重写父类方法)
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
if (i == 1) {
FootballMatch f = new FootballMatch(sc.next());
f.compete();
} else if (i == 2) {
BasketBallMatch b = new BasketBallMatch(sc.next());
b.compete();
}
懒人一键copy
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
if (i == 1){
FootballMatch f = new FootballMatch(sc.next());
f.compete();
}else if(i == 2){
BasketBallMatch b =new BasketBallMatch(sc.next());
b.compete();
}
}
}
//父类球类比赛
class BallMatch{
public void compete(){
checkin();
start();
end();
}
//第一步 球员报道
public void checkin(){
System.out.println("now checking in");
}
//第二步 比赛开始
public void start(){
System.out.println("now starting");
}
// 第四步 比赛结束
public void end(){
System.out.println("now ending");
}
}
//足球比赛继承父类球类比赛
class FootballMatch extends BallMatch{
String score;
public FootballMatch(String score) {
this.score = score;
}
//定义公共的方法
@Override
public void compete() {
checkin();
start();
play();
end();
annouceResult();
}
//足球比赛
public void play(){
System.out.println("now playing football");
}
//足球比赛得分
public void annouceResult(){
System.out.println("now annoucing result: "+score);
}
}
class BasketBallMatch extends BallMatch{
String score;
public BasketBallMatch(String score) {
this.score = score;
}
//重写父类的公共方法并增加子类独有的play()和annouceResult()方法
@Override
public void compete() {
checkin();
start();
play();
end();
annouceResult();
}
//篮球比赛
public void play(){
System.out.println("now playing basketball");
}
//篮球比赛得分
public void annouceResult(){
System.out.println("now annoucing result: "+score);
}
}