特俗在:
实体类例子:
public class Student {
//必须私有成员变量,并为每个变量生成get,set方法
private String name;
private double score;
//必须有一个公开的无参构造器
public Student(){
}
public Student(String name, double score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
?实体类就是用来保存某个事务的数据的容器
实体类应用场景:
当一个类里面既封装了存储数据的相关方法,又封装了处理数据的相关方法时,可以对这些职责进行拆分,存储数据用一个类(JavaBean),处理数据用另外一个类,以实现数据存储和数据业务处理相分离
拿上面的学生类举例写一个学生操作类:
public class StudentOperator {
private Student student;
public StudentOperator(Student student){
this.student = student;
}
public void Pass_judgment(){
System.out.println(this.student.getScore() >= 60 ? "及格" : "不及格");
}
}
测试一下:
public class Test {
public static void main(String[] args) {
Student s = new Student("泽予",60);
StudentOperator op = new StudentOperator(s);
op.Pass_judgment();
}
}
运行结果: