Class คือ แบบพิมพ์ หรือเทมเพลตที่กำหนดโครงสร้าง (ฟิลด์) และพฤติกรรม (เมธอด) Object (หรือ instance) คือสิ่งที่เป็นรูปธรรมที่สร้างจากแบบพิมพ์นั้น โดยมีค่าของมันเองในหน่วยความจำ
แบบพิมพ์เทียบกับอินสแตนซ์
text
class Car ← ONE definition (the blueprint)
│ fields: color, speed
│ methods: accelerate()
▼
new Car("red") ← many OBJECTS (instances), each with its own state
new Car("blue")
ในโค้ด
python
:
():
.color = color
.speed =
():
.speed +=
a = Car()
b = Car()
a.accelerate()
(a.speed, b.speed)
(a.color b.color)
