物件導向——執行個體講解:類設計分析

來源:互聯網
上載者:User

 

本章目標:
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());
}
};

總結:
題目的分析要一點點的慢慢的積累。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.