一、變數 1、實例變數(又叫欄位、屬性) 創建對象時給對象賦值 形式: self.xxx = xxx 訪問: 對象名.xxx 只能由對象訪問 1 class Person: 2 def __init__(self,age,name): 3 self.name = name #實例變數 4 self. ...
一、變數
1、實例變數(又叫欄位、屬性)
創建對象時給對象賦值
形式:
self.xxx = xxx
訪問:
對象名.xxx 只能由對象訪問
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Person: 2 def __init__(self,age,name): 3 self.name = name #實例變數 4 self.age = age #實例變數 5 p1 = Person(18,"iboy") 6 print(p1.name) #通過對象名訪問實例變數 7 8 p1.hobby = "打游戲" #這是在當前類中添加一個實例變數 9 print(p1.hobby)View Code
2、類變數
直接寫在類中的變數,給類賦值
形式:
變數名 = 值
訪問:
類名/對象名.xxx 類名和對象名都能訪問,但是只能通過類名來修改變數值。通過對象名修改,相當於在當前對象中增加了一個實例變數
一般把對象中的共性抽出來作為類變數
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Person: 2 country = "中國" #類變數 3 def __init__(self,age,name): 4 self.name = name #實例變數 5 self.age = age #實例變數 6 7 p1 = Person("18","iboy") #對象p1 8 print(p1.country) #中國 9 p2 = Person("17","jacklove") #對象p2 10 print(p2.country) #中國 11 print("--------") 12 Person.country = "中華" #通過類名 修改了類變數country 13 print(p1.country) #中華 14 print(p2.country) #中華 15 print("--------") 16 p1.country = "大清" #通過對象名 是在p1中創建了實例變數country, 並沒有修改類變數country 17 print(p1.country) #大清 18 print(p2.country) #中華View Code
二、方法
1、實例方法
直接寫在類中的方法,只能由對象調用
形式:
def 方法名(self,參數):
pass
訪問:
對象名.方法名(參數)
1 class Car: 2 def run(self): 3 print("車會跑") 4 def cul(self,a,b): 5 print(a+b) 6 def jump(self): 7 print("you jump,i push") 8 #Car.run() #TypeError: run() missing 1 required positional argument: 'self' 9 c = Car() #創建對象c 10 c.run() 11 c.cul(521,1314) 12 c.jump() 13 14 結果: 15 車會跑 16 1835 17 you jump,i push
2、類方法
在聲明時加上@classmethod裝飾的方法
形式:
@classmethod
def 方法名(cls):
pass
訪問:
類名/對象名.方法名()
class Person: def chi(self): #實例方法 print("人要吃飯") @classmethod def he(cls): # 這是類方法,可以通過類和對象名訪問 print(cls) print("人要喝水") Person.he() p = Person() p.he() 結果: <class '__main__.Person'> 人要喝水 <class '__main__.Person'> 人要喝水
3、靜態方法
聲明時加@staticmethod 裝飾的方法,相當於在類中定義的一個普通函數
形式:
@staticmethod
def 方法名():
pass
訪問:
類名/對象名.方法名()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Person: def chi(self): # 實例方法 print("人在吃") # 類方法 @classmethod # 類方法 def he(cls): # cls 類 print(cls) print("我是喝") @staticmethod def sleep(): # 在類中定義的一個普通函數,不帶參 print("和你睡不等於睡你 -- 薑文") @staticmethod def fly(height): # 在類中定義的一個普通函數,帶參 print("上天%s" % height) Person.sleep() Person.fly(500) p = Person p.sleep() p.fly(500) 結果: 和你睡不等於睡你 -- 薑文 上天500 和你睡不等於睡你 -- 薑文 上天500靜態方法
三、屬性方法
通過@property 把一個方法變成一個實例變數來使用,我自稱為屬性方法,就是本來是一個方法,但是有屬性的效果。
形式:
@property
def 方法名(self):
return 值
訪問:
對象名.方法名
class Person: def __init__(self,name,birthday,qq): self.name = name self.birthday = birthday self.qq = qq @property def age(self): return 2018-self.birthday p1 = Person("王三",1995,"19252862163") #print(p1.age()) # TypeError: 'int' object is not callable age是不可調用的 print(p1.age) # 23 可以像屬性一樣用 print(Person.age) # 通過類名訪問訪問不到 <property object at 0x0000000001E18EF8>
!!!註意:
函數只能有一個self 參數
函數必須有返回值
不能給該屬性賦值 像p1.age = 10 是不行的
四、私有
在變數名或方法名前面加__作為首碼就表示這是私有的
私有的東西只能類自己內部訪問
1 class Person: 2 def __init__(self, name): # 構造, 創建對象的時候自動調用 3 self.__name = name # 私有的 4 5 def __chi(self): # 私有的 6 print("我要吃. 瘋狂的吃") 7 8 def he(self): 9 self.__chi() # 內部調用 10 print("我是喝", self.__name) 11 12 # Person.__chi #類訪問私有方法 報錯 AttributeError: type object 'Person' has no attribute '__chi' 13 p = Person("哈哈哈") 14 # p.__chi() #對象訪問私有方法 報錯 AttributeError: 'Person' object has no attribute '__chi' 15 #print(p.__name) #對象訪問私有實例變數 報錯 AttributeError: 'Person' object has no attribute '__name' 16 p.he() #內部訪問 可以 17 18 結果: 19 我要吃. 瘋狂的吃 20 我是喝 哈哈哈
需要註意的是, 對於私有的內容,子類是無法繼承的。