从一个简单的小学生信息管理程序就可一看出对java思想的理解程度
1.第一个版本:
是用数组来存数学生信息的,没有用到类似C语言的结构体,也没有用到java的类的封装对象的思想。简单的用数组来存储,然后显示出来
而且可以写在一个类里面,挤成一坨!
public class TestScore {
public static void main(String[] args) {
//声明一个数组变量
int
scores[]=new int[5];
float total=0;//总成绩
float avg=0;//平均分
int
max=0;
int
min=0;
//扫描器
Scanner input=new Scanner(System.in);
System.out.println("请输入5名学生的成绩:");
for(int i=0;i
scores[i]=input.nextInt();
}
//计算总成绩,平均分,最高分,最低分
max=scores[0];
min=scores[0];
//总成绩
for(int j=0;j
total+=scores[j];
//最高分
if(scores[j]>max);{
max=scores[j];
}
//最低分
if(scores[j]
min=scores[j];
}
}
//平均分
avg=total / scores.length;
System.out.println("总成绩是"+total);
System.out.println("最高分是"+max);
System.out.println("最低分是"+min);
System.out.println("平均分是"+avg);
}
}
2.第二LOW的版本:
就是貌似用了java的的分类思想,把学生信息变量单独写了一个类
classStudent{
privateint id;
privateString name;
.....
}
但实质和第一LOW版本没什么区别,只不多把学生信息分开声明了个类,但没有用到java的核心思想。
例:
当然,这些内容也都可以写在一个类里面
3.真正算java的版本:
1.要有java面向对象的思想,首先把学生类Student.java单独分出来
2.然后实际管理层又是一个类,主函数在这里面
3.其中student类中用set/get方法来封装各个变量,使其成为各个对象,完成java面向对象的实现
总结:
把一组数据结构和处理他们的方法组成对象(Object)
把相同行为的对象归为类(class)
通过类的封装(encapsulation)隐藏内部细节
通过继承实现类的特化/泛化
通过多态实现基于对象类型的动态分配
这是写一个java程序之前要考虑的事情