Java基础学习(七)下

常用到类就是实体类,其实就是对数据的封装,下面是新建一个User类的代码

package com.abc.entity;
import java.io.Serializable;
/**
* 编写一个实体类:用户表(User)
* 1、类要实现对象序列化
* 2、无参构造方法
* 3、全参构造方法
* 4、各个属性的get()、set()方法
* 5、重写toString()方法
* 6、方法的五要素:修饰词、返回值类型,方法名、参数列表、方法体。在定义类的时候,注意属性私有化,方法公开化,另外对于一些数据进行特殊逻辑初始化处理,如年龄,邮箱等。
 */
public class User implements Serializable {
//   定义属性
private long userId;//用户ID
private String userName;//用户名
private String userPwd;//用户密码
private String userCode;//用户编号
private String userRank;//用户职级
private String userEmail;//用户邮箱
private String userPhone;//用户手机
private int userAge;//用户年龄
private String userPost;//用户岗位
private double userSalary;//用户薪资

//默认无参构造方法
public User() {
    super();
}

//全参数构造方法
public User(long userid, String userName, String userPwd, String userCode, String userRank, String userEmail, String userPhone, int userAge, String userPost, double userSalary) {
    this.userId = userId;
    this.userName = userName;
    this.userPwd = userPwd;
    this.userCode = userCode;
    this.userRank = userRank;
    this.userEmail = userEmail;
    this.userPhone = userPhone;
    this.userAge = userAge;
    this.userPost = userPost;
    this.userSalary = userSalary;
}

//   get()方法
public long getUserId() {
    return userId;
}

public String getUserName() {
    return userName;
}

public String getUserPwd() {
    return userPwd;
}

public String getuserCode() {
    return userCode;
}

public String getUserRank() {
    return userRank;
}

public String getUserEmail() {
    return userEmail;
}
public String getUserPhone(){
    return userPhone;
}
public int getUserAge(){
    return userAge;
}
public String getUserPost(){
    return userPost;
}
public double getUserSalary(){
    return userSalary;
}
//set()方法
public void setUserId(long userId){
    this.userId = userId;
}
public void setUserName(String userName){
    this.userName = userName;
}
public void setUserPwd(String userPwd){
    this.userPwd = userPwd;
}
public void setUserCode(String userCode){
    this.userCode = userCode;
}
public void setUserRank(String userRank){
    this.userRank = userRank;
}
public void setUserEmail(String userEmail){
    if(userEmail.endsWith("@abc.com") || userEmail.endsWith("@qq.com") || userEmail.endsWith("@163.com")){
        this.userEmail = userEmail;//支持邮箱的三种格式
    }else {
        System.out.println("输入的邮箱不合法");
    }

}
public void setUserPhone(String userPhone){
    this.userPhone = userPhone;
}
public void setUserAge(int userAge){
    if(userAge>0 && userAge<100) {
        this.userAge = userAge;
    }else {
        System.out.println("年龄不合法,请重新输入!");
    }
}
public void setUserPost(String userPost){
    this.userPost = userPost;
}
public void setUserSalary(double userSalary){
    this.userSalary = userSalary;
}
//重写toString方法

@Override
public String toString() {
    return "User{" +
            "userId=" + userId +
            ", userName='" + userName + '\'' +
            ", userPwd='" + userPwd + '\'' +
            ", userCode='" + userCode + '\'' +
            ", userRank='" + userRank + '\'' +
            ", userEmail='" + userEmail + '\'' +
            ", userPhone=" + userPhone +
            ", userAge=" + userAge +
            ", userPost='" + userPost + '\'' +
            ", userSalary=" + userSalary +
            '}';
	}
}

你可能感兴趣的:(java)