Java实体类的构建与使用

  • 构造方法: 与实体类同名的方法,用于创建实体类对象。
  • set方法: 为实体类对象中的属性赋值
  • get方法: 从实体类对象中获取属性值

构造方法所带参数不同,则创建的对象“长相”也不同。
一个实体类可以有很多个,“长相”不同的对象。

package com.auko.test.entity;

/**
 * 实体类Student,包含属性stu_name,class_no,stu_no,tel_no,math_grade,chinese_grade
 * */
public class Student {
    private String stu_name;
    private String class_no;
    private int stu_no;
    private int tel_no;
    private double math_grade;
    private double chinese_grade;

    /**
     * 构造方法,用于创建对象
     * */
    /*全参构造方法*/
    public Student(String stu_name, String class_no, int stu_no, int tel_no, double math_grade, double chinese_grade) {
        this.stu_name = stu_name;
        this.class_no = class_no;
        this.stu_no = stu_no;
        this.tel_no = tel_no;
        this.math_grade = math_grade;
        this.chinese_grade = chinese_grade;
    }

    /*按需创建非全参构造方法*/
    public Student(String stu_name, String class_no, int tel_no) {
        this.class_no = class_no;
        this.stu_name = stu_name;        
        this.tel_no = tel_no;
    }

    /*空参构造方法*/
    public Student() {
    }

    /**
     * set方法:为实体类对象的属性赋值
     * */
    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }

    public void setClass_no(String class_no) {
        this.class_no = class_no;
    }

    public void setStu_no(int stu_no) {
        this.stu_no = stu_no;
    }

    public void setTel_no(int tel_no) {
        this.tel_no = tel_no;
    }

    public void setMath_grade(double math_grade) {
        this.math_grade = math_grade;
    }

    public void setChinese_grade(double chinese_grade) {
        this.chinese_grade = chinese_grade;
    }

    /**
     * get方法:从实体类对象中获取属性值
     * */
    public String getStu_name() {
        return stu_name;
    }

    public String getClass_no() {
        return class_no;
    }

    public int getStu_no() {
        return stu_no;
    }

    public int getTel_no() {
        return tel_no;
    }

    public double getMath_grade() {
        return math_grade;
    }

    public double getChinese_grade() {
        return chinese_grade;
    }
}

/*创建对象*/
Student stu1 = new Student(s_name, c_no, s_no, t_no, math_g, chinese_g);
Student stu2 = new Student(s_name, c_no, t_no);
Student stu3 = new Student();

IDEA快速生成上述方法:alt+insert
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值