Object - Oriented Programming

Untitled

把properties and behaviours綁定到individual object上面

例如list’s properties = length, behaviour = add/remove

my_list = [1, 2, 3, 4]
print(type(my_list))

#<class 'list'>
#class就是data type

class is a code template for creating object

我們可以設定class class裡面設定好這個物件是什麼

基本propertie and behavior會先列出來

class Robot:
    pass

my_robot = Robot()
print(type(my_robot))

#<class '__main__.Robot'>
#class是剛才定義的Robot

[Python物件導向]淺談Python類別(Class) https://www.learncodewithmike.com/2020/01/python-class.html

Untitled

class Robot:
    # constructor
    def __init__(self, name, age):
        self.name = name
        self.age = age

my_robot_1 = Robot("Wilson", 25)
print(my_robot_1.name)
print(my_robot_1.age)
class Robot:
    # constructor
    def __init__(self, name, age):
        self.name = name
        self.age = age

my_robot_1 = Robot("Wilson", 25)
my_robot_2 = Robot("Grace", 26)
print(my_robot_1.age < my_robot_2.age)

#True