本章目標:
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());
}
};
總結:
題目的分析要一點點的慢慢的積累。