1. 程式人生 > >建立多個物件

建立多個物件

class Cat:
# 屬性
# 方法
def eat(self):
print(“貓在吃魚…”)

def drink(self):
    print("貓在喝可樂...")

def introduce(self):
    # print("名字是:%s, 年齡是:%d" % (湯姆的名字, 湯姆的年齡))
    print("名字是:%s, 年齡是:%d" % (tom.name, tom.age))

#建立了一個物件
tom = Cat()
tom.name = “湯姆”
tom.age = 30
tom.eat()
tom.drink()
print(tom.name

)
print(tom.age)
print("-"*30)
tom.introduce()

print("="*30)

#建立了另外一個物件
lan_mao = Cat()
lan_mao.name = “藍貓”
lan_mao.age = 20
lan_mao.introduce()
通過一個類,可以建立多個物件,就好比 通過一個模具建立多個實體一樣