一、程序分析思路:
1:根据要求写出类所包含的属性
2:所有的属性都必须进行封装
3:封装之后的属性通过setter和getter设置和取得
4:如果需要可愿意加入若干构造方法:
5:再根据其他要求添加相应的方法
6:类中的所有方法都不要直接输出,而是交给被调用处输出
二、实例分析
class Student
{
private String stuno;
private String name;
private float math;
private float english;
private float computer;
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 ExampleDemo
{
public static void main(String args[]){
Student stu = null;
stu = new Student("1000","历史",45.0f,67.0f,89.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.getComputer());
System.out.println("最高分"+stu.max());
System.out.println("最低分"+stu.min());
System.out.println("平均分"+stu.avg());
System.out.println("总分"+stu.sum());
}
}
题目的分析要一点一点积累;