JAVA入門學習-學習如何自定義一個實體類,用於儲存一組資料。

gongjinsi發表於2017-05-16

實體類:由下大致理解為:因為萬物皆物件,所以我們可以把一個實體或一個事物抽象成類,就成了實體類。
然後因為我們資料都是存在資料庫中,所以通過實體類,建立資料層和邏輯層的聯絡。

引用一下別人在實體類的經驗

一、實體類的名字儘量和資料庫的表的名字對應相同。
二、實體類應該實現java.io.Serializable介面。
三、實體類應該有個無參的構造方法。
四、實體類應該有個有參(所有的引數)的構造方法。
五、實體類有屬性和方法,屬性對應資料庫中表的欄位,方法主要有getter和setter方法。
六、實體類還應該有個屬性serialVersionUID。
例如:private static final long serialVersionUID = -6125297654796395674L;
七、屬性一般是private型別,方法為public型別,對於資料庫自動生成的ID欄位對應的屬性的set方法為private。

話不多說,實踐見真知:
目標就建立一個會診的實體類,再儲存一組會診資料。

public class consultation {
    private int consultationId;
    private int applicant;
    private String applyTime;
    private String departmentName;
    private String departmentNo;
    private String diagnosis;
    private String endTime;
    private short memberCount;
    private String orgCode;
    private String orgName;
    private byte patientAge;
    private String patientBedno;
    private String patientDepartment;
    private byte patientGender;
    private String patientHospital;
    private String patientName;
    private String patientSno;
    private byte patientSnoType;
    private String purpose;
    private String sessionId;
    private String title;
    private byte consultationstatus;
    public consultation() { //無參構造方法
    }
    public consultation(int consultationId, int applicant, String applyTime, String departmentName, String departmentNo,
            String diagnosis, String endTime, short memberCount, String orgCode, String orgName, byte patientAge,
            String patientBedno, String patientDepartment, byte patientGender, String patientHospital,
            String patientName, String patientSno, byte patientSnoType, String purpose, String sessionId, String title,
            byte consultationstatus) {   //有參構造方法
        super();
        this.consultationId = consultationId;
        this.applicant = applicant;
        this.applyTime = applyTime;
        this.departmentName = departmentName;
        this.departmentNo = departmentNo;
        this.diagnosis = diagnosis;
        this.endTime = endTime;
        this.memberCount = memberCount;
        this.orgCode = orgCode;
        this.orgName = orgName;
        this.patientAge = patientAge;
        this.patientBedno = patientBedno;
        this.patientDepartment = patientDepartment;
        this.patientGender = patientGender;
        this.patientHospital = patientHospital;
        this.patientName = patientName;
        this.patientSno = patientSno;
        this.patientSnoType = patientSnoType;
        this.purpose = purpose;
        this.sessionId = sessionId;
        this.title = title;
        this.consultationstatus = consultationstatus;
    }

    public int getConsultationId() {
        return consultationId;
    }
    public int getApplicant() {
        return applicant;
    }
    public String getApplyTime() {
        return applyTime;
    }
    public String getDepartmentName() {
        return departmentName;
    }
    public String getDepartmentNo() {
        return departmentNo;
    }
    public String getDiagnosis() {
        return diagnosis;
    }
    public String getEndTime() {
        return endTime;
    }
    public short getMemberCount() {
        return memberCount;
    }
    public String getOrgCode() {
        return orgCode;
    }
    public String getOrgName() {
        return orgName;
    }
    public byte getPatientAge() {
        return patientAge;
    }
    public String getPatientBedno() {
        return patientBedno;
    }
    public String getPatientDepartment() {
        return patientDepartment;
    }
    public byte getPatientGender() {
        return patientGender;
    }
    public String getPatientHospital() {
        return patientHospital;
    }
    public String getPatientName() {
        return patientName;
    }
    public String getPatientSno() {
        return patientSno;
    }
    public byte getPatientSnoType() {
        return patientSnoType;
    }
    public String getPurpose() {
        return purpose;
    }
    public String getSessionId() {
        return sessionId;
    }
    public String getTitle() {
        return title;
    }
    public byte getConsultationstatus() {
        return consultationstatus;
    }

    private void setConsultationId(int consultationId) {
        this.consultationId = consultationId;
    }
    private void setApplicant(int applicant) {
        this.applicant = applicant;
    }
    private void setApplyTime(String applyTime) {
        this.applyTime = applyTime;
    }
    private void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
    private void setDepartmentNo(String departmentNo) {
        this.departmentNo = departmentNo;
    }
    private void setDiagnosis(String diagnosis) {
        this.diagnosis = diagnosis;
    }
    private void setEndTime(String endTime) {
        this.endTime = endTime;
    }
    private void setMemberCount(short memberCount) {
        this.memberCount = memberCount;
    }
    private void setOrgCode(String orgCode) {
        this.orgCode = orgCode;
    }
    private void setOrgName(String orgName) {
        this.orgName = orgName;
    }
    private void setPatientAge(byte patientAge) {
        this.patientAge = patientAge;
    }
    private void setPatientBedno(String patientBedno) {
        this.patientBedno = patientBedno;
    }
    private void setPatientDepartment(String patientDepartment) {
        this.patientDepartment = patientDepartment;
    }
    private void setPatientGender(byte patientGender) {
        this.patientGender = patientGender;
    }
    private void setPatientHospital(String patientHospital) {
        this.patientHospital = patientHospital;
    }
    private void setPatientName(String patientName) {
        this.patientName = patientName;
    }
    private void setPatientSno(String patientSno) {
        this.patientSno = patientSno;
    }
    private void setPatientSnoType(byte patientSnoType) {
        this.patientSnoType = patientSnoType;
    }
    private void setPurpose(String purpose) {
        this.purpose = purpose;
    }
    private void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }
    private void setTitle(String title) {
        this.title = title;
    }
    private void setConsultationstatus(byte consultationstatus) {
        this.consultationstatus = consultationstatus;
    }
    public static void main(String[] args) {
        // 測試該會診實體類
        SimpleDateFormat sdf = new  SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        consultation con=new consultation(1, 10086, sdf.format(new Date().getTime()), "測試科室", "10001",
                "直腸癌","",(short)2,"123456789", "測試醫院",(byte)53,
                "5床", "重症醫學科",(byte)1, "人民醫院",
                "測試病人1", "123456",(byte) 2, "確認是否為直腸癌","zaqwsx", "測試病人1的會診",(byte)0);
        System.out.println("====="+con.getTitle()+"=====");
        System.out.println("由"+con.getOrgName()+" "+con.getDepartmentName()+"的"+con.getApplicant()+"發起會診");
        System.out.println("會診病人為"+con.getPatientHospital()+" "+con.getPatientDepartment()+"的"+con.getPatientName());
        System.out.println("已初步診斷該患者為"+con.getDiagnosis()+",並請求"+con.getPurpose());
        System.out.println("=======結束=======");

        Scanner scan=new Scanner(System.in);
        String status=null;;
        do{
            System.out.println("是否同意接受會診(Y/N)");
            status=scan.nextLine();
        }while((status.equals("Y")||status.equals("y")||status.equals("N")||status.equals("n"))==false);
        if(status.equals("Y")||status.equals("y"))
            con.setConsultationstatus((byte)1);
        else;
        if(con.getConsultationstatus()==(byte)1)
            System.out.println("已接受會診");
        else System.out.println("未接受會診");
    }
}
/*
*輸出:
=====測試病人1的會診=====
由測試醫院 測試科室的10086發起會診
會診病人為人民醫院 重症醫學科的測試病人1
已初步診斷該患者為直腸癌,並請求確認是否為直腸癌
=======結束=======
是否同意接受會診(Y/N)
hafjskfh
是否同意接受會診(Y/N)
Y
已接受會診
*/

相關文章