声明一个运动员类Athlete,类属性:姓名,性别,个人得分,团队得分(使用静态),无参构造方法,带参构造方法,一个得分方法(个人得分自加一,团队得分自加一)。测试类,通过带参构造方法实例化三个对象。并每个对象执行一两次得分方法。最后输出个人得分,以及团队得分。
package com.lxl.test04;
/**
* 运动员类
* @author lxl
*
*/
public class Athlete {
String name;
char sex;
/** 个人得分 */
int score;
/** 团队得分 */
static int teamScore;
public Athlete(){
}
public Athlete(String name,char sex,int score,int teamScore){
this.name =name;
this.sex =sex;
this.score =score;
this.teamScore =teamScore;
}
public void getSco(){
score++;
teamScore++;
}
}
package com.lxl.test04;
public class Test {
public static void main(String[] args) {
Athlete lxl = new Athlete("林晓丽",'女',0,0);
Athlete sgf = new Athlete("小米",'女',0,0);
Athlete ttr = new Athlete("本宝宝",'女',0,0);
lxl.getSco();
sgf.getSco();
ttr.getSco();
ttr.getSco();
System.out.println("个人得分");
System.out.println(lxl.name+","+lxl.score);
System.out.println(sgf.name+","+sgf.score);
System.out.println(ttr.name+","+ttr.score);
System.out.println("团队得分");
System.out.println(Athlete.teamScore);
}
}