类是一个蓝图或模板,定义了结构(字段)和行为(方法)。对象(或实例)是从该蓝图构建的具体事物,在内存中有自己的值。
蓝图 vs 实例
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)
