面向对象-实体类(javabean)

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());
    }




}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值