“一切皆对象,对象就是表格”,这句话比较绝对,在一般情况来说我们就可以这样的理解。
数据无处不在,而表格就是存储这些数据最常用的办法。打比方说某件物品的大小、型号、价格…这些最常见的数据,其实也都是表格中的每一个数据,如图所示:
图中是学生及学生成绩表格,包含学号、姓名、性别、各科成绩及总分,我们可以看作是类中的属性,动作则是类中的方法,如图所示:
要记得不同类型的使用方式,在这里姓名和性别使用了String字符串,其他比如学号,成绩等数字则使用了int类型。
这样一看,每个表格就是一个类,表格的标题,就是类中的属性,而方法则是每个学生对象都需要完成的事情。
代码如下
public class Student {
int id ;
String name ;
String sex ;
int chinese_score;
int math_score;
int english_score;
int total_score;
void fun(){
System.out.println("7.30到校打卡晨读");
}
public Student(){}
public Student(int id, String name, String sex, int chinese_score,
int math_score, int english_score, int total_score)
{
this.id = id;
this.name = name;
this.sex = sex;
this.chinese_score = chinese_score;
this.math_score = math_score;
this.english_score = english_score;
this.total_score = total_score;
}
@Override
public String toString() {
return "学号:"+id+"\t姓名:"+name+"\t性别:"+sex
+"\t语文:"+chinese_score+"\t数学:"+math_score
+"\t英语"+english_score +"\t总分:"+total_score;
}
public static void main(String[] args) {
Student s1 = new Student(1,"甲","男",
80,95,85,260);
System.out.println(s1);
s1.fun();
}
}
运行结果:
由此可见,数据信息都可以表格的形式存在,表格可以看作一个类,类中的数据就是对象。