Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ
George Boorman
Curriculum Manager, DataCamp
| คำศัพท์ | ความหมาย |
|---|---|
| Class | แบบพิมพ์เขียว/เทมเพลตสำหรับสร้างออบเจกต์ |
| Object | การรวมกันของ ข้อมูล และ ฟังก์ชันการทำงาน คือ instance ของ class |
| คำศัพท์ | ความหมาย |
|---|---|
| Class | แบบพิมพ์เขียว/เทมเพลตสำหรับสร้างออบเจกต์ |
| Object | การรวมกันของ ข้อมูล และ ฟังก์ชันการทำงาน คือ instance ของ class |
| State | ข้อมูล ที่เชื่อมกับออบเจกต์ กำหนดผ่าน attribute |
| Behavior | ฟังก์ชันการทำงาน ของออบเจกต์ นิยามผ่าน method |
| ตัวดำเนินการ | Method |
|---|---|
== |
__eq__() |
!= |
__ne__() |
>= |
__ge__() |
<= |
__le__() |
> |
__gt__() |
< |
__lt__() |
__str__()print(obj), str(obj)print([1,2,3])
[1 2 3]
str([1,2,3])
'[1, 2, 3]'
__repr__()repr(obj), การแสดงผลใน consolerepr([1,2,3])
[1,2,3]
[1,2,3]
[1,2,3]
print() เมื่อไม่มี __str__()class BalanceError(Exception): passclass Customer: def __init__(self, name, balance): if balance < 0 : raise BalanceError("Balance has to be non-negative!") else: self.name, self.balance = name, balance# Use try-except to catch errors try: cust = Customer("Larry Torres", -100) except BalanceError: cust = Customer("Larry Torres", 0)
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ