//另一种方法
public classStudent {static int id=0;//学生序号//用类变量保留相应科目最高(低)分的ID,便于对象共享
static int mathScoreMaxID=0;static int mathScoreMinID=0;static int englishScoreMaxID=0;static int englishScoreMinID=0;static int computerScoreMaxID=0;static int computerScoreMinID=0;static int totalScoreMaxID=0;static int totalScoreMinID=0;//用类变量保留相应科目最高(低)分,便于对象共享
private static float mathScoreMax=0;//数学科目最高分
private static float englishScoreMax=0;private static float computerScoreMax=0;private static float mathScoreMin=100;private static float englishScoreMin=100;private static float computerScoreMin=100;private static float totalScoreMax=0;private static float totalScoreMin=0;//将成员变量定义为私有的,体现面向对象的封装性
private String studentNumber;//学生学号
privateString studentName;privateString studentSex;privateString studentMajor;private floatmathScore;private floatenglishScore;private floatcomputerScore;private floattotalScore;private floataverageScore;//构造方法,创建对象时调用
Student(String number,String name,String sex,String major){
id++;//每创建一个Students对象,则序号值增1;
studentNumber=number;
studentName=name;
studentSex=sex;
studentMajor=major;
}//给每个成员变量(各个科目成绩)赋值
void setAllScore(int id,float math,float english,floatcomputer){
setMathScore(id,math);
setEnglishScore(id,english);
setComputerScore(id,computer);
setTotalScore();
setAverageScore();
}//给数学成绩赋值,用类变量保存最大(小)值以及相应序号
void setMathScore(int id,floatmath){
mathScore=math;if(math>mathScoreMax){//获得数学分最高的学生序号
mathScoreMax=math;
mathScoreMaxID=id;
}if(math
mathScoreMin=math;
mathScoreMinID=id;
}
}void setEnglishScore(int id ,floatenglish ){
englishScore=english;if(english>englishScoreMax){//获得英语分最高的学生序号
englishScoreMax=english;
englishScoreMaxID=id;
}if(english
englishScoreMin=english;
englishScoreMinID=id;
}
}void setComputerScore(int id,floatcomputer){
computerScore=computer;if(computer>computerScoreMax){//获得计算机分最高的学生序号
computerScoreMax=computer;
computerScoreMaxID=id;
}if(computer
computerScoreMin=computer;
computerScoreMinID=id;
}
}voidsetTotalScore(){
totalScore=mathScore+englishScore+computerScore;if(totalScore>totalScoreMax){
totalScoreMax=totalScore;
totalScoreMaxID=id;
}if(totalScore
totalScoreMin=totalScore;
totalScoreMinID=id;
}
}voidsetAverageScore(){
averageScore=totalScore/3;
}//学号、姓名查找方法
booleanfindStudentNumber(String number){returnstudentNumber.equals(number);
}booleanfindStudentName(String name){returnstudentName.equals(name);
}//以下方法主要由于外部访问内部私有变量
floatgetMathScore(){returnmathScore;
}floatgetEnglishScore(){returnenglishScore;
}floatgetComputerScore(){returncomputerScore;
}
String getStudentNumber(){returnstudentNumber;
}
String getStudentName(){returnstudentName;
}
String getStudentSex(){returnstudentSex;
}
String getStudentMajor(){returnstudentMajor;
}
}