class People(object): # 類屬性是指定義在類的內部而且在方法的外部的屬性 money = 10000 def __init__(self,name,age,gender=1): # 對象屬性是指定義在方法的內部的屬性,例如本例中 # name,age和gender都是對象屬性 self.name = name self.age = age self.gender = gender # 創建兩個類的對象 student1 = People("張三",20) student2 = People("李四",25) 類屬性和對象屬性的區別: # 對象可以通過 對象名.屬性名 調用對象屬性和類屬性 print(student2.name) print(student2.money) # 而類也可以通過 類名.屬性名 調用類的屬性,但是 # 不能通過這種方式調用對象的屬性 # 例如類調用name屬性,會報異常 # AttributeError: type object 'People' has no attribute 'name' print(People.money) print(People.name)
類屬性和對象屬性在使用上的區別:
# 在進行運算前這三個引用的都是類屬性money # 所以這三個的屬性money的內存地址都是相同的 print(id(student1.money)) print(id(student2.money)) print(id(People.money) # 進行如下的運算 student1.money -= 1000 People.money -= 1000 # 再打印三者的內存地址 print(id(student1.money)) print(id(student2.money)) print(id(People.money))
會發現student1引用的money屬性的內存地址已經和另外兩個的不一樣了而另外兩個的內存地址卻還是一樣的,原因如下:
在經過表達式student1.money -= 1000 的過程如下:第一次引用money屬性時,經歷的過程如下:會先在對象中查找是否有money這個屬性,如果有的話,則直接進行運算如果沒有,則會去類中查找是否有money屬性,如果在類中找到money屬性,那么student1就會創建一個對象屬性money,在第二次調用的時候就會調用自己的對象屬性,而不是類People中的屬性了,而student2因為沒有經過運算,所以不會創建自己的money屬性,而是引用類People的屬性,所以student2和People引用的還是同一個屬性
# 當student2進行同樣的運算后,那么student2中也會創建一個money屬性 # 此時三者的money屬性的內存地址都不一樣了 student2.money -= 1000
print(id(student1.money))
print(id(student2.money))
print(id(People.money))