Object-Oriented Programming(物件導向)
my_list = [1, 2, 3, 4]
print(type(my_list))
----------------------------------------------------
<class 'list'>
class Robot:
pass
my_robot = Robot()
print(type(my_robot))
----------------------------------------------------
<class '__main__.Robot'>
class Robot:
ingrediant = "metal" # 2. 改放在class下面
def __init__(self, name, age):
self.name = name
self.age = age
#self.ingrediant = "metal" # 1. 這邊不太適合
print(Robot.ingrediant)
----------------------------------------------------
metal
可抓取global variable
Class attributes can be accessed by either
Inside a method, when referring to the class itself, try to use self.class as nuch as possible. Avoid hard code the class name.
→ 若寫在class name下,只要class name修改,全部使用到此class name的都要改
self.class.attribute
static method
class method
補充 : 使用static method和class method的差別