การห่อหุ้ม
สำรวจแนวคิดขั้นสูงของการเขียนโปรแกรมเชิงวัตถุ
การห่อหุ้ม เป็นบทเรียน Python For Kids ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 5 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Python For Kids และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Python For Kids มีบทเรียนทั้งหมด 5 บทเรียน
การห่อหุ้ม
ยินดีต้อนรับกลับสู่บทเรียนการเขียนโปรแกรมเชิงวัตถุ (OOP) วันนี้เราจะเรียนรู้เรื่องการห่อหุ้มใน Python ซึ่งเป็นแนวคิดสำคัญที่ช่วยปกป้องข้อมูลและเมธอดภายในคลาส

การห่อหุ้มคืออะไร
การห่อหุ้มคือแนวคิดในการรวมข้อมูล (แอตทริบิวต์) และเมธอด (ฟังก์ชัน) ที่ทำงานกับข้อมูลนั้นไว้ในหน่วยเดียวกัน ซึ่งเรียกว่าคลาส นอกจากนี้ยังจำกัดการเข้าถึงส่วนประกอบบางอย่างของออบเจ็กต์โดยตรง ทำให้รายละเอียดการทำงานภายในของออบเจ็กต์ถูกซ่อนไว้จากภายนอก
- การซ่อนข้อมูล: ปกป้องสถานะภายในของออบเจ็กต์จากการรบกวนที่ไม่ได้ตั้งใจ
- การควบคุมการเข้าถึง: กำหนดวิธีเข้าถึงหรือแก้ไขข้อมูลและเมธอด
การห่อหุ้มช่วยให้แน่ใจว่าออบเจ็กต์สามารถควบคุมข้อมูลของตนเอง และเปิดเผยเฉพาะสิ่งที่จำเป็น
ตัวควบคุมการเข้าถึง
ใน Python การห่อหุ้มใช้งานผ่านตัวควบคุมการเข้าถึง เพื่อควบคุมการมองเห็นสมาชิกของคลาส (แอตทริบิวต์และเมธอด) ตัวควบคุมการเข้าถึงมีสามประเภท:
- สาธารณะ: เข้าถึงได้จากทุกที่
- ได้รับการปกป้อง: เข้าถึงได้ภายในคลาสและคลาสย่อย
- ส่วนตัว: เข้าถึงได้เฉพาะภายในคลาสนั้นเอง
มาดูวิธีใช้ตัวควบคุมการเข้าถึงเหล่านี้ใน Python กัน
แอตทริบิวต์และเมธอดสาธารณะ
สมาชิกสาธารณะสามารถเข้าถึงได้จากภายนอกคลาส โดยค่าเริ่มต้น แอตทริบิวต์และเมธอดทั้งหมดจะเป็นสาธารณะ เว้นแต่จะระบุเป็นอย่างอื่น
ตัวอย่าง:
# Defines the Person class with public attributes and methods
class Person:
def __init__(self, name, age):
self.name = name # Public attribute
self.age = age # Public attribute
def greet(self):
print("Hello, my name is " + self.name + "!") # Public method
# Creates an object of the Person class
person1 = Person("Alice", 30)
# Accesses public attributes and methods
print(person1.name) # Output: Alice
person1.greet() # Output: Hello, my name is Alice!
แอตทริบิวต์และเมธอดที่ได้รับการปกป้อง
สมาชิกที่ได้รับการปกป้องจะแสดงด้วยขีดล่างหนึ่งขีด (_) นำหน้า สมาชิกเหล่านี้เข้าถึงได้ภายในคลาสและคลาสย่อย แต่ไม่ควรเข้าถึงโดยตรงจากภายนอกคลาส
ตัวอย่าง:
# Defines the Employee class with protected attributes and methods
class Employee:
def __init__(self, name, salary):
self._name = name # Protected attribute
self._salary = salary # Protected attribute
def _increase_salary(self, amount):
self._salary += amount # Protected method
print(self._name + "'s new salary is " + str(self._salary))
# Creates an object of the Employee class
employee1 = Employee("Bob", 50000)
# Accesses protected attributes and methods (not recommended)
print(employee1._name) # Output: Bob
employee1._increase_salary(5000) # Output: Bob's new salary is 55000
แอตทริบิวต์และเมธอดส่วนตัว
สมาชิกส่วนตัวจะแสดงด้วยขีดล่างสองขีด (__) นำหน้า สมาชิกเหล่านี้เข้าถึงได้เฉพาะภายในคลาสนั้นเอง และไม่สามารถเข้าถึงโดยตรงจากภายนอกคลาส
ตัวอย่าง:
# Defines the BankAccount class with private attributes and methods
class BankAccount:
def __init__(self, owner, balance):
self.__owner = owner # Private attribute
self.__balance = balance # Private attribute
def deposit(self, amount):
self.__balance += amount # Modifies private attribute
print("Deposited:", amount)
self.__show_balance()
def __show_balance(self):
print("Current Balance:", self.__balance) # Private method
# Creates an object of the BankAccount class
account1 = BankAccount("Charlie", 1000)
# Attempts to access private attributes and methods (will cause errors)
# print(account1.__owner) # AttributeError
# account1.__show_balance() # AttributeError
# Uses public methods to interact with private attributes
account1.deposit(500) # Output:
# Deposited: 500
# Current Balance: 1500
เหตุใดจึงต้องใช้การห่อหุ้ม
การห่อหุ้มมีประโยชน์หลายประการ:
- การควบคุม: ควบคุมวิธีเข้าถึงหรือแก้ไขข้อมูล
- ความปลอดภัย: ปกป้องสถานะภายในของออบเจ็กต์จากการเปลี่ยนแปลงที่ไม่ได้ตั้งใจ
- การดูแลรักษา: ทำให้โค้ดดูแลรักษาได้ง่ายขึ้นและมีโอกาสเกิดข้อผิดพลาดน้อยลง
การห่อหุ้มช่วยให้สร้างแอปพลิเคชันที่มีความแข็งแกร่งและปลอดภัย
ตัวอย่างการห่อหุ้ม: คลาส Car
มาสร้างคลาส Car ที่ใช้การห่อหุ้มเพื่อปกป้อง attributes และ methods กันครับ
โค้ด:
# Defines the Car class with encapsulated attributes and methods
class Car:
def __init__(self, make, model, year):
self.make = make # Public attribute
self.model = model # Public attribute
self.__year = year # Private attribute
def get_year(self):
return self.__year # Public method to access private attribute
def set_year(self, year):
if year > 0:
self.__year = year # Public method to modify private attribute
print("Year updated to", self.__year)
else:
print("Invalid year!")
def __display_info(self):
print(self.make, self.model, self.__year) # Private method
def show_info(self):
self.__display_info() # Public method that calls a private method
# Creates an object of the Car class
car1 = Car("Tesla", "Model S", 2020)
# Accesses public attributes and methods
print(car1.make) # Output: Tesla
print(car1.get_year()) # Output: 2020
# Modifies the private attribute using a public method
car1.set_year(2021) # Output: Year updated to 2021
# Calls a public method that interacts with a private method
car1.show_info() # Output: Tesla Model S 2021
# Attempts to access private attributes and methods directly (will cause errors)
# print(car1.__year) # AttributeError
# car1.__display_info() # AttributeError
# Defines the Student class with encapsulated attributes
class Student:
def __init__(self, name, grade):
self.name = name # Public attribute
self.__grade = grade # Private attribute
def get_grade(self):
return self.__grade # Public method to access private attribute
def set_grade(self, grade):
if 0 <= grade <= 100:
self.__grade = grade # Updates the grade
else:
print("Invalid grade!")
# Creates an object of the Student class
student1 = Student("Diana", 85)
# Accesses and modifies the grade using public methods
print(student1.get_grade()) # Output: 85
student1.set_grade(90)
print(student1.get_grade()) # Output: 90
# Attempts to access the private attribute directly (will cause an error)
# print(student1.__grade) # AttributeError
เก่งมากครับ!
คุณได้เรียนรู้วิธีใช้การห่อหุ้มใน Python เพื่อปกป้องข้อมูลและ methods ภายในคลาสต่าง ๆ แล้ว การห่อหุ้มช่วยให้คุณควบคุมการเข้าถึงข้อมูลของออบเจ็กต์ ทำให้โปรแกรมมีความปลอดภัยและดูแลรักษาได้ง่ายยิ่งขึ้น ฝึกฝนต่อไปด้วยการออกแบบคลาสที่มีตัวกำหนดสิทธิ์การเข้าถึงอย่างเหมาะสม เพื่อพัฒนาทักษะการเขียนโค้ดของคุณครับ!

คำถามที่พบบ่อย
บทเรียน “การห่อหุ้ม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การห่อหุ้ม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Python For Kids ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Python For Kids มีบทเรียนทั้งหมด 5 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การห่อหุ้ม”
สำรวจแนวคิดขั้นสูงของการเขียนโปรแกรมเชิงวัตถุ คุณปฏิบัติ Python For Kids ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Python For Kids หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Python For Kids บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 5 บทเรียน
บทเรียน “การห่อหุ้ม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Python For Kids นี้ได้ไหม
ได้ บทเรียน Python For Kids ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ