Intro to OOP

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 Attribute, Static Method and Class Method

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

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的都要改