类设计分析

来源:互联网 发布:wdcp如何选择php版本 编辑:程序博客网 时间:2024/05/17 21:47


类设计分析

 类设计分析思路:

  • 根据要求写出类所包含的属性

  • 所有属性都必须封装

  • 封装之后的属性通过settergetter方法设置和取得

  • 如果需要可以加入若干构造方法

  • 再根据其他要求加入相应方法

  • 类中所有的方法都不要直接输出,而是交被调用处输出 

 题目:定义一个名为Student的类,包括属性有“学号”、“姓名”以及3门课程“数学”、“英语”和“计算机”的成绩,包括的方法有计算3门课程的“总数”、“平均分”、“最高分”和“最低分”

1.类中的属性及类型

 

序号

 

属性名称

1

学号

String

stuno

2

姓名

String

name

3

数学成绩

float

math

4

英语成绩

float

english

5

计算机成绩

float

computer

 

2.定义出需要的方法(普通方法、构造方法)

        在本例设计2个构造方法,一个无参的构造方法,另外一个构造方法可以为5个属性赋值。

序号

方法名称

返回值类型

作用

1

public void setStuno(String s)

void

设置学生编号

2

public void setName(String n)

void

设置学生姓名

3

public void setMath(float m)

void

设置数学成绩

4

public void setEnglish(float e)

void

设置英语成绩

5

public void setComputer(float c)

void

设置计算机成绩

6

public String getStuno()

String

取得学生编号

7

public String getName()

String

取得学生姓名

8

public float getMath()

float

取得数学成绩

9

public float getEnglish()

float

取得英语成绩

10

public float getComputer()

float

取得计算机成绩

11

public float sum()

float

计算成绩总和

12

public float avg()

float

计算平均成绩

13

public float max()

float

求出最高成绩

14

public float min()

float

求出最低成绩

15

public Student(){}

-

无参构造方法

16

Public Student(String stuno,String name,float math,float english,float computer)

-

在对象实例化时直接将学号、姓名、数学成绩、英语成绩、计算机成绩设置进去

 

3.类图

Student

  • stuno      String

  • name       String

  • math       float

  • english   float

  • computer  float

+ <<Constructor>>  Student()

+ <<Constructor>> Student(String stuno,String name,floatmath,float english,float computer)

+                 setStuno(String s)

+                 setName(String n)

+                 setMath(float m)

+                 setEnglish(float e)

+                 setComputer(float c)

+                 getStuno()

+                 getName()

+                 getMath()

+                 getEnglish()

+                 getComputer()

+                 sum()

+                 avg()

+                 max()

+                 min()

 

4.范例:实现代码

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 ;    }    public 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 min = math ;   // 数学是最高成绩       min = min<computer?min:computer ;       min = min<english?min:english ;       return min ;    }};public class ExampleDemo01{    public static void main(String args[]){       Student stu = null ;        // 声明对象       stu = new Student("001","李兴华",95.0f,89.0f,96.0f) ;       System.out.println("学生编号:" + stu.getStuno()) ;       System.out.println("学生姓名:" + stu.getName()) ;       System.out.println("数学成绩:" + stu.getMath()) ;       System.out.println("英语成绩:" + stu.getEnglish()) ;       System.out.println("最高分:" + stu.max()) ;       System.out.println("最低分:" + stu.min()) ;    }};

---根据李兴华老师视频总结

0 0
原创粉丝点击