简单的综合的写了一个demo
package cn.xuexi; /* * 作业: * 定义一个测试类为student,属性有“学号”,“姓名”,“数学成绩”,英语成绩,计算机成绩,其中包括的方法有总分,平均分,最高分,最低分 * 分析思路: * 1、写出类中包含的属性 * 2、所有的属性都必须封装,并通过setter getter调用 * 3、添加成员方法计算总成绩,平均分,最高分,最低分 * 4、写一个构造函数为类中属性赋值 */ public class ZongHeDemo { public static void main(String[] args) { student s = new student("fhuehf","2011x",89.9f,35.5f,43.6f); System.out.println(s.getid()+"****"+s.getname()+"****"+s.avg()+"****"+s.max()+"****"+s.min()); } } class student{ //申明类中属性 private String name; private String id; private float math; private float english; private float computer; public void setname(String name) { this.name = name; } public void setid(String id) { this.id = id; } public void setmath(float math) { this.math = math; } public void setenglish(float english) { this.english = english; } public void setcomputer(float computer) { this.computer = computer; } public String getname() { return name; } public String getid() { return id; } public float getmath() { return math; } public float getenglish() { return english; } public float getcomputer() { return computer; } public float sum() { float sum; sum = (this.getmath()+this.getcomputer()+this.getenglish()); return sum; } public float avg() { float av; av = (this.getmath()+this.getcomputer()+this.getenglish())/3; return av; } public float max() { float max; if (math>english){ max = math; } else { max = english; } if (computer>max) { max = computer ; return max; } else { return max; } } public float min() {float min; if (math>english){ min = english; } else { min = math; } if (computer>min) { return min; } else { min = computer ; return min; } } //写一个构造方法,为类中属性初始化 public student(String name,String id,float math,float english,float computer) { this.setcomputer(computer); this.setenglish(english); this.setmath(math); this.setname(name); this.setid(id); } }