Encapsulation: private fields, getters, setters · Encapsulation: ฟیلด์แบบ private, getters, setters
Protect your data
- Encapsulation means hiding an object's data and only changing it through methods.
- This keeps the data safe. Other code cannot put a bad value into it.
- The main tools are the words
privateandpublic.
ปกป้องข้อมูลของคุณ
- Encapsulation หมายความว่าซ่อนข้อมูลของ object และแก้ไขผ่าน metMethod เท่านั้น
- สิ่งนี้ทำให้ข้อมูลปลอดภัย โค้ดอื่นไม่สามารถใส่ค่าที่ไม่ถูกต้องเข้าไปในนั้นได้
- เครื่องมือหลักคือคำว่า
privateและpublic
private fields
- Mark instance variables
privateso code outside the class cannot touch them directly. - Methods inside the same class can still use them.
- This is the normal style for AP CSA: fields are
private.
ฟิลด์ private
- กำหนดให้ตัวแปรของอินสแตนซ์เป็น
privateเพื่อให้โค้ดภายนอกคลาส ไม่สามารถเข้าถึงโดยตรง - MetMethod ภายในคลาสเดียวกันยังคงสามารถใช้พวกมันได้
- นี่คือรูปแบบมาตรฐานสำหรับ AP CSA: ฟิลด์ควรเป็น
private
public class BankAccount {
private double balance; // hidden from outside code
public BankAccount(double start) {
this.balance = start;
}
}
Accessors (getters)
- A field is private, so we add a
publicmethod to read it. - A method that returns a field is called an accessor or getter.
- It usually starts with
get, likegetBalance().
Accessors (getters)
- ฟิลด์เป็นแบบ private ดังนั้นเราจึงเพิ่ม metMethod
publicเพื่อ อ่าน ค่ามัน - MetMethod ที่ return ฟิลด์เรียกว่า accessor หรือ getter
- โดยปกติจะเริ่มด้วย
getเช่นgetBalance()
public class BankAccount {
private double balance;
public BankAccount(double start) {
this.balance = start;
}
public double getBalance() {
return this.balance;
}
}
Mutators (setters)
- A method that changes a field is called a mutator or setter.
- It can check the new value first and refuse a bad one.
- Below,
depositignores a negative amount, so the balance stays safe.
Mutators (setters)
- MetMethod ที่ เปลี่ยนแปลง ฟิลด์เรียกว่า mutator หรือ setter
- มันสามารถ ตรวจสอบ ค่าใหม่ก่อนและปฏิเสธค่าที่ไม่ดีได้
- ด้านล่างนี้,
depositจะเพิกเฉยต่อจำนวนลบ ทำให้ยอดคงเหลือปลอดภัย
public class BankAccount {
private double balance;
public BankAccount(double start) {
this.balance = start;
}
public void deposit(double amount) {
if (amount > 0) {
this.balance = this.balance + amount;
}
}
public double getBalance() {
return this.balance;
}
}
static vs instance
- An instance field belongs to one object. Each object has its own.
- A
staticfield belongs to the class itself — it is shared by all objects. - Below,
countrises by 1 every time anyRobotis built.
static เทียบกับ instance
- ฟิลด์ instance เป็นของ object เดียวเท่านั้น แต่ละ object มีของมันเอง
- ฟิลด์
staticเป็นของ คลาสเอง — มันถูก แชร์ โดยทุก object - ด้านล่างนี้,
countเพิ่มขึ้นทีละ 1 ทุกครั้งที่ anyRobotถูกสร้าง
public class Robot {
private static int count = 0; // shared by all robots
public Robot() {
count = count + 1;
}
public static int getCount() {
return count;
}
}
toString()
toString()returns text that describes the object.- Java calls it automatically when you print the object or join it with
+. - It takes no parameters and returns a
String.
toString()
toString()return ข้อความที่อธิบายเกี่ยวกับ object- Java เรียกใช้มันโดยอัตโนมัติเมื่อคุณพิมพ์ object หรือรวม它与
+ - มันไม่รับ parameters และ return
String
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "(" + this.x + ", " + this.y + ")";
}
}
Common mistakes
- Make fields
privateand reach them through getters and setters. - A setter can check a value before storing it.
ข้อผิดพลาดที่พบบ่อย
- กำหนดให้ฟิลด์เป็น
privateและเข้าถึงผ่าน getters และ setters - Setter สามารถตรวจสอบค่าก่อนจัดเก็บ
Now you try
- Fill in each class so its private data is changed only through safe methods.
- A hidden Harness builds the object, calls the methods, and checks the results.
- Press Run to compile, then Check answer.
ลองดูเลย
- เติมแต่ละคลาสให้ข้อมูลส่วนตัวถูกเปลี่ยนแปลง เฉพาะ ผ่าน metMethod ที่ปลอดภัย
- Harness ที่ซ่อนอยู่สร้าง object, เรียก metMethod และตรวจสอบผลลัพธ์
- กด Run เพื่อคอมไพล์ แล้วกด Check answer
Complete BankAccount. Keep balance private. deposit(amount) adds the amount only if it is greater than 0. withdraw(amount) subtracts only if the amount is greater than 0 and not more than the balance (no overdraft). getBalance() returns the balance. · เติม BankAccount ให้สมบูรณ์. เก็บ balance ไว้เป็น private. deposit(amount) เพิ่มจำนวนเฉพาะเมื่อมันมากกว่า 0. withdraw(amount) ลบออกเฉพาะเมื่อจำนวนนั้นมากกว่า 0 และ ไม่เกินยอดคงเหลือ (ไม่มีกรณี透支). getBalance()/value กลับคืนยอดคงเหลือ.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete Temperature. The constructor stores an int degrees in a private field. toString() returns the degrees followed by C — for 21 it returns 21C. · เติม Temperature ให้สมบูรณ์. คอนสตรัคเตอร์เก็บ int degrees ในฟیلด์แบบ private. toString()/value กลับคืนองศาตามด้วย C — สำหรับ 21 จะกลับคืน 21C.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete Widget. A private static field count starts at 0. Every time a Widget is built, the constructor adds 1 to count. The static method getCount() returns how many widgets have been built. · เติม Widget ให้สมบูรณ์. มีฟیلด์ static ชื่อ count เริ่มต้นที่ 0. ทุกครั้งที่สร้าง instances ของ Widget, คอนสตรัคเตอร์จะเพิ่ม 1 เข้าไปใน count. เมทโด้ static getCount()/value กลับคืนว่ามีการสร้าง widget ไปแล้วกี่ชิ้น.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่