类的设计
一、设计一个学生类,里面有学生的三项成绩:计算机成绩、数学成绩、英语成绩
要求可以求总分、平均分、最高分、最低分,并且可以输出一个学生的完整信息,问:此类该如何设计? 程序开发步骤:
1、 根据要求定义出所要的类
2、 根据题目中的要求规划出类的属性:name、age、computer、english、math 3、 所有的属性必须封装:private
4、 所有的属性必须通过getter及setter访问 5、 需要增加构造方法,为属性赋值
6、 所有的信息不要在类中直接输出,而是交给调用处输出
在类中不能出现Sysetm.out.print()语句子 class Student{ private String name ; private int age ; private float english ; private float computer ; private float math ; public Student(){} public Student(String n,int a,float e,float c,float m){ this.setName(n) ; this.setAge(a) ; this.setEnglish(e) ; this.setComputer(c) ; this.setMath(m) ; } public float sum(){ return english + computer + math ; } public float avg(){ return this.sum() / 3 ; } public float max(){ float max = computer>math?computer:math ; max = max>english?max:english ; return max ; } public float min(){ float min = computer