Classes & objects · คลาส & วัตถุ (Classes & objects)
A class is a blueprint
- A class defines a new kind of thing — a user-defined type.
- An object is one item made from that class.
- One
Dogclass can make many dog objects, each with its own data.
Class คือแบบร่าง
- Class กำหนดประเภทของสิ่งใหม่ — User-defined type
- Object คือหนึ่งรายการที่ทำจาก class นั้น
- One
Dogclass สามารถสร้างหลาย object ของสุนัข ได้แต่ละตัวมีข้อมูลของตัวเอง
init and attributes
__init__is a special method that runs when you make an object.selfrefers to the object being made.- Values stored on
selfare the object's attributes (its data).
init และ attributes
__init__เป็น special method ที่รันเมื่อคุณสร้าง objectselfชี้ถึง objectที่กำลังถูกสร้าง- Values stored on
selfคือ attributes ของ object (data ของมัน)
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
p = Point(2, 5)
print(p.x)
print(p.y)
Methods
- A method is a function defined inside a class.
- Its first parameter is always
self, the object it works on. - Call it with a dot:
object.method().
Methods
- Method คือฟังก์ชันที่นิยามไว้ใน class
- Parameter แรกของมันคือ
selfเสมอ ซึ่งเป็น object ที่มันทำงานด้วย - เรียก它以 dot:
object.method()
class Greeter:
def __init__(self, name):
self.name = name
def hello(self):
return "Hi, " + self.name
g = Greeter("Sam")
print(g.hello())
Enumerated types
- An enumerated type is a fixed set of named values.
from enum import Enumlets you define one as a class.- It is clearer than using loose numbers like
1,2,3.
Enumerated types
- Enumerated type คือชุดคงที่ของ named values
from enum import Enumทำให้คุณสามารถกำหนด它以 class- It is clearer than using loose numbers like
1,2,3
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
print(Color.GREEN)
print(Color.GREEN.value)
In Cambridge pseudocode
- A
CLASSblock lists its data and aNEWconstructor.
ใน伪代码 Cambridge
- A
CLASSblock lists its data and aNEWconstructor
CLASS Point
PRIVATE X : INTEGER
PRIVATE Y : INTEGER
PUBLIC PROCEDURE NEW(startX : INTEGER, startY : INTEGER)
X ← startX
Y ← startY
ENDPROCEDURE
ENDCLASS
DECLARE p : Point
p ← NEW Point(2, 5)
Common mistakes
- Every method takes
selfas its first parameter. __init__runs automatically when you create an object.
ข้อผิดพลาดที่พบบ่อย
- Every method takes
selfas its first parameter __init__runs automatically when you create an object
Now you try
- Define each class with
__init__and any methods it needs. - Press Check answer to test your code.
ลองดูเลย
- นิยามแต่ละคลาสด้วย
__init__และเมทโอดที่จำเป็น - กด Check answer เพื่อทดสอบโค้ดของคุณ
Define a class Dog whose __init__ takes name and age and stores them as attributes self.name and self.age. · กำหนดคลาส Dog ที่ __init__ รับ name และ age และเก็บไว้เป็น attributes self.name และ self.age
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Define a class Rectangle with __init__(self, width, height) and a method area(self) that returns width * height. · กำหนด class Rectangle ที่มี __init__(self, width, height) และ method area(self) ที่ return width * height
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Define a class BankAccount. __init__ sets self.balance to 0. Add deposit(self, amount) to add money and withdraw(self, amount) to take it away. · กำหนดคลาส BankAccount __init__ ตั้งค่า self.balance เป็น 0 เพิ่ม deposit(self, amount) เพื่อเพิ่มเงิน และ withdraw(self, amount) เพื่อดึงเงินออก
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่