本章目标:
1:掌握类的基本分析思路
2“应用思路分析具体的题目
程序分析思路
在具体题目讲解之前先给读者一些分析的思路:
1:根据要求写出类所包含的属性;
2:所有的属性都必须进行封装;
3:封装之后的属性通过sette和getter设置和取得;
4:如果需要可以加入若干构造方法;
5:再根据其他要求添加相应的方法;
6:类中的所有方法都不要直接输出,而是交给被调用处输出。
----------------------------------------------------
题目要求:
顶定义并测试一个名为Student的类,包括属性有“学号”“姓名”以及3门课
程“数学”“英语”和“计算机”的成绩,包括的方法有计算3门的课程的“
总分”“平均分”“最高分贝”和“最低分”。
例子:
class Student{
private String stuno;
private String name;
private float math;
private float english;
private float computer;
public student(){} //构造方法
public student(string s,string n,float m,float e,float c){ //构造方
法
this.setStuno(s);
this.setName(n);
this.setMath(m);
this.setEnglish(e);
this.setComputer(c);
}
public void setStuno(String s){
stuno=s;
}
public void setName(String n){
name=n;
}
public void setMath(float m){
math=m;
}
public void setEnglish(float e){
english=e;
}
public void setComputer(float c){
computer=c;
}
public string getStuno(){
return stuno;
}
public string getName(){
return name;
}
public float getMath(){
return math;
}
public float getEnglish(){
return english;
}
oublic float getComputer(){
return computer;
}
public float sum(){ //求和操作
return math + english + computer;
}
public float avg(){ //求平均值
return this.sum()/3;
}
public float max(){ //求最高成绩
float max = math; //数学是最高成绩
max = max>computer?max:computer;
max = max>english?max:english;
return max;
}
public float min(){ //求出最低成绩
float max = math; //数学是最低成绩
min = min<computer?max:computer;
min = min<english?max:english;
return min;
}
public class ExampleDemo01{
public static void main(String args[]){
student stu=null; //声明对象
stu = new student("MLND-33","李兴华",95.of,89.of,10:20 2011-11-
1710:20 2011-11-17
96.of);
System.out.println("学生编号:"+stu.getStuno());
System.out.println("学生姓名:"+stu.getName());
System.out.println("数学成绩:"+stu.getMAth());
System.out.println("英语成绩:"+stu.getEnglish());
System.out.println("计算机成绩:"+stu.getComputer());
System.out.println("最高分:"+stu.max());
System.out.println("最低分:"+stu.min());
}
};
总结:
题目的分析要一点点的慢慢的积累。