1. 程式人生 > 其它 >7. SpringBoot校驗

7. SpringBoot校驗

Java封裝

  • 封裝(資料的隱藏):通常,應禁止直接訪問一個物件中資料的實際表示,而應通過操作介面來訪問,這稱為資訊隱藏
  • private:屬性私有
  • get:獲得這個資料
  • set:給這個資料設定值
package com.oop.demo04;

//類         private:私有
public class Student {

    //屬性私有
    private String name;//名字
    private int id;//學號
    private char sex;//性別

    //提供一些可以操作這個屬性的方法!
    //提供一些public的get、set方法

    //alt+fn+ins快捷鍵生成
    //get 獲得這個資料
    public String getName() {
        return name;
    }
    //set 給這個資料設定值
    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }
}
package com.oop;

import com.oop.demo04.Student;

//一個專案應該只存在一個main方法
public class Application {

    public static void main(String[] args) {
        Student s1 = new Student();

        s1.setName("shun");

        System.out.println(s1.getName());//輸出的結果是:shun
    }

}