1.什么是实体类
1.成员变量必须私有,且要为他们提供get,set方法;必须有无参构造方法
2.仅仅只是一个用来保存数据的java类,可以用它创建对象,保存某个事物的数据
2.实体类有什么应用场景
实体类的对象只负责数据存取,而对数据的业务处理交给其他类的对象来完成,以实现数据和数据业务相分离
3.实际代码
test类:
package ithema.javabean;
public class Test {
public static void main(String[] args) {
//实体类的基本作用,存取数据
Student s1 =new Student("bonus",100.00,100.00);
/*
* 实体类在实际开发中的场景
* 实体类的对象只负责数据存取,而对数据的业务处理交给其他类的对象来完成,以实现数据和数据业务相分离
* */
StudentServie studentServie = new StudentServie(s1);
studentServie.printmath();
}
}
实体类:
package ithema.javabean;
public class Student {
//创建实体类
private String name;
private Double chinase;
private Double math;
//创建set和get方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getChinase() {
return chinase;
}
public void setChinase(Double chinase) {
this.chinase = chinase;
}
public Double getMath() {
return math;
}
public void setMath(Double math) {
this.math = math;
}
//创建有参构造函数
public Student(String name, Double chinase, Double math) {
this.name = name;
this.chinase = chinase;
this.math = math;
}
//创建无参构造函数
public Student() {
}
}
service类:
package ithema.javabean;
public class StudentServie {
//实际操作层
private Student student;
//获取test类的实际数据值,并赋给成员变量
public StudentServie(Student student) {
this.student = student;
}
//成员变量取到值以后,调用实体类的getMath方法
public void printmath(){
System.out.println(student.getMath());
}
}
157

被折叠的 条评论
为什么被折叠?



