Object Oriented Programming (OOP)
Table of Contents
- 1) Python Classes & Objects
- 2) Designing a class
- 3) Create a class
- 4) Create an object
- 5) Constructors in Python (the __init__() method)
- 6) The __str__() method
- 7) [รู้หรือไม่] Create object from class in separate file
- 8) Passing object as parameter
- 9) Exercise
1) Python Classes & Objects
- A class is a blueprint/template that is used to construct objects.
- Each object is called an instance of the class.
2) Designing a class
- When you design a class, think about the objects that will be created from that class.
- Things the object knows about itself (เรียกว่า instances or attributes)
- Things the object does (เรียกว่า methods)
🍪🍬🍭🍦Try!! (1) รู้ไหมว่า list
และ string
ก็เป็นคลาส
- จะเห็นว่าทั้ง
list
และstring
เป็นคลาส - บรรทัดที่ 1 และ 2 เราได้สร้าง
object
ของคลาสlist
และstring
ขึ้นมาappend
เป็น method หนึ่งของlist
lower
เป็น method หนึ่งของstring
- เราจะได้รู้จัก
list
,string
และ methods ต่าง ๆ เพิ่มเติมในบทที่ 3
3) Create a class
- To create a class, use the keyword class
🍪🍬🍭🍦Try!! (2)
- เราจะได้คลาส Dog มาหนึ่งคลาส แต่เรายังไม่ได้สร้างออบเจ็กต์ จึงไม่มีการแสดงผลใด ๆ ออกมา
- attribute หรือ instance ของคลาส ก็คือ ตัวแปรของคลาส
- method ของคลาส ก็คือ ฟังก์ชันภายในคลาส
4) Create an object
- The procedure to create an object is similar to a function call.
🍪🍬🍭🍦Try!! (3)
- รันโค้ดด้านล่าง ซึ่งเป็นตัวอย่างการสร้างออบเจ็กต์ของคลาส Dog ชื่อ d1 และเรียกใช้ attribute และ method
- สังเกตผลลัพธ์ที่ได้
- ทดลองปรับเพิ่มโค้ด โดยการสร้างออบเจ็กต์และเรียกใช้เพิ่มเติม
5) Constructors in Python (the __init__() method)
- method that begins with double underscore (__) are called special methods as they have special meaning.
- __init__() เป็น method พิเศษซึ่งจะถูกเรียกให้ทำงานอัตโนมัติเมื่อมีการสร้างออบเจ็กต์ใหม่
- ในการเขียนโปรแกรมเชิงวัตถุ (OOP) เราเรียก method แบบนี้ว่า constructor
- เรามักใช้ constructor สำหรับกำหนดค่าเริ่มต้นให้กับตัวแปรของคลาส
ตัวอย่าง:
- หากเราเพิ่ม constructor ให้กับคลาส Dog จะได้ดามโค้ดด้านล่าง และสังเกตว่าการสร้างออบเจ็กต์ก็จะเปลี่ยนไป นั่นคือ เราต้องระบุ arguments ด้วย
class Dog:
def __init__(self, c, w):
self.color = c
self.size = w
def bark(self):
print('Ruff Ruff')
d1 = Dog('black', 10)
print(d1.color)
print(d1.size)
black
10
6) The __str__() method
- returns the string representation of the object.
- ส่งค่ากลับเป็นสตริงของออบเจ็กต์นั้นๆ ซึ่งเราสามารถระบุได้
class Dog:
def __init__(self, c, w):
self.color = c
self.size = w
def bark(self):
print('Ruff Ruff')
d1 = Dog('black', 10)
print(d1)
<catsoop.base_context.Dog object at 0x7f21f2f54040>
class Dog:
def __init__(self, c, w):
self.color = c
self.size = w
def __str__(self):
return 'Dog:'+str(self.color)+':'+str(self.size)
def bark(self):
print('Ruff Ruff')
d1 = Dog('black', 10)
print(d1)
Dog:black:10
🙋Help!! โค้ดด้านล่างนี้ยังรันไม่ผ่าน โปรดช่วยแก้ไขโดย
- เพิ่ม constructor method
- เมื่อรันผ่านแล้ว ทดลองเปลี่ยนแปลงค่าสตริงใน __str__() ตามต้องการ แล้วรันดูผลลัพธ์อีกครั้ง
- ทดลองปรับเพิ่มโค้ด โดยการสร้างออบเจ็กต์, เรียกใช้ method เพิ่มเติม, สร้าง method อื่น ๆ เพิ่มเติม
7) [รู้หรือไม่] Create object from class in separate file
- เราสามารถสร้างคลาสแยกไว้อีกไฟล์หนึ่งได้ โดยใช้แนวคิดเดียวกันกับการสร้างและเรียกใช้โมดูล
8) Passing object as parameter
- เป็นการส่งค่าข้อมูลซึ่งในที่นี้คือ ออบเจ็กต์ ไปให้กับ method
- ตัวอย่างในสไลด์เป็นการสร้างไฟล์คลาสแยกออกมา ซึ่งในที่นี้ ตัวอย่างด้านล่าง เราจะสร้างคลาสและสร้างออบเจ็กต์ใน cell เดียว (หรือจะแยก cell ก็ได้, แนวคิดเดียวกับตอนเรียนเรื่องฟังก์ชัน)
🍪🍬🍭🍦Try!! (4)
- รันโค้ดด้านล่าง ซึ่งเป็นการสร้างคลาส Bank ที่มีเมธอดชื่อ transfer ซึ่งจะเห็นว่าในการเรียกใช้งานจะต้องระบุชื่อออบเจ็กต์เพื่อส่งให้กับตัวแปร dest
- สังเกตผลลัพธ์ที่ได้
- ทดลองปรับเพิ่มโค้ด โดยการสร้างออบเจ็กต์เพิ่มเติม
9) Exercise
Exercise 1
จากคลาส Student ที่กำหนดให้ จงเขียนโค้ดเพิ่มเติม ตามที่แจ้งใน TODO (อย่าลืมลบคำสั่ง pass ด้วย)
Exercise 2
จากคลาส Car ที่กำหนดให้ จงเขียนโค้ดเพิ่มเติม ตามที่แจ้งใน TODO (อย่าลืมลบคำสั่ง pass ด้วย)
Exercise 3
จากคลาส Cat ที่กำหนดให้ จงเขียนโค้ดเพิ่มเติม ตามที่แจ้งใน TODO (อย่าลืมลบคำสั่ง pass ด้วย)