這篇文章主要介紹python面向?qū)ο笾泻瘮?shù)有什么用,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
兩當ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
面向?qū)ο?/strong>
面向?qū)ο缶幊獭狾bject Oriented Programming,簡稱OOP,是一種程序設(shè)計思想。OOP把對象作為程序的基本單元,一個對象包含了數(shù)據(jù)和操作數(shù)據(jù)的函數(shù)
class Car: #靜態(tài)字段,類屬性 cars='車的種類之一' def __init__(self,name): #動態(tài)字段,實例屬性 self.name=name car1=Car('寶馬') print(car1.name) print(Car.cars) print(car1.cars)
定義1個類:class xxx:
初始化1個對象:def __init__(self,value)
__init__ 是初始化的意思
定義Car(車)這個類,而寶馬這個實例對象擁有 name屬性
通過字典,一次性取出實例對象所有屬性 __dict__
class People: def __init__(self,name,sex,number,age): self.name=name self.sex=sex self.number=number self.age=age p1=People('SBharmel','man',1350023,16) print(p1.__dict__) #通過字典,一次性取出,對象所有屬性 #輸出結(jié)果: #{'name': 'SBharmel', 'number': 1350023, 'sex': 'man', 'age': 16}
從屬關(guān)系
cars 屬于 Car 類 類不能訪問動態(tài)字段
name 屬于 car1 對象 對象可以訪問動態(tài)字段和靜態(tài)字段
只有是從屬關(guān)系,才能用 .xxx 方法獲取
所以可以寫 car.cars #獲取靜態(tài)字段 car1.name #獲取動態(tài)字段
如果寫成 car.name 則會報錯
不能寫的原因:
1.從屬關(guān)系不同
2. 車這個類可能擁有多個類型如 寶馬, 奔馳 他們兩個的屬性不一樣,如價格,車速不同
#但是對象可以獲取 靜態(tài)字段 car1.cars
因為 car1 也屬于 這個類 (car)
class car: def __init__(self,name,speed,price): self.name=name self.speed=speed self.price=price car1=car('寶馬','130km/h','100w') car2=car('奔馳','140km/h','80w') print(car1.name) #正確 print(car2.price) #正確 print(car.speed) #報錯 ''' Traceback (most recent call last): File "E:\workspace\day4\backend\class.py", line 23, in <module> print(car.speed) AttributeError: type object 'car' has no attribute 'speed' '''
靜態(tài)字段和動態(tài)字段
類創(chuàng)建的字段叫做 靜態(tài)字段 cars
.sefl 創(chuàng)建的字段叫做 動態(tài)字段 name
def () 一般寫在類里面的叫做方法,寫在類之外的叫做函數(shù)
靜態(tài)方法 使用類或?qū)ο竺苯釉L問的方法
@staticmethod #構(gòu)造的函數(shù)()里面不傳參數(shù)self,調(diào)用寫(),類和對象都可以調(diào)用
類方法 使用類或?qū)ο竺苯釉L問的方法
@classmethod #構(gòu)造的函數(shù)()里面不傳參數(shù)self ,但要寫cls,調(diào)用寫(),類和對象都可以調(diào)用
特性
@property #構(gòu)造的函數(shù) ()里面要傳參數(shù) self,調(diào)用不用寫(),僅對象可調(diào)用
#僅為實例對象的特性,類調(diào)用無內(nèi)容,相當于實例化特有的屬性
class Student: # 靜態(tài)字段,類屬性 classtype = "學生" # 初始化 實例屬性,動態(tài)字段 def __init__(self,name,age): self.name = name self.age = age # 動態(tài)方法,實例方法 def eat(self): print("學生在吃飯") # 靜態(tài)方法 @staticmethod def staticfun(): print("這是一個靜態(tài)方法") # 類方法 @classmethod def cm(cls): print("這是一個類方法") # 特性 @property def properfun(self): print("properfun:我的特性,是一個學生,勸你好自為之") print("-----------類可以調(diào)用的類容------------------------------------") print(Student.classtype) Student.staticfun() Student.cm() Student.properfun # 這么寫不報錯,但也不打印,Student.properfun() 報錯 print(Student.__dict__) print("-----------實例化對象后可以調(diào)用的類容--------------------------") stu1 = Student("lee",18) print(stu1.classtype) print(stu1.name) print(stu1.age) stu1.eat() Student.eat(stu1) stu1.staticfun() stu1.cm() stu1.properfun #特性調(diào)用不用加(),它相當于實例化的一個屬性 print(stu1.__dict__) ''' 打印的結(jié)果 -----------類可以調(diào)用的類容------------------------------------ 學生 這是一個靜態(tài)方法 這是一個類方法 {'__module__': '__main__', 'classtype': '學生', '__init__': <function Student.__init__ at 0x000002323C96A1F0>, 'eat': <function Student.eat at 0x000002323C96A280>, 'staticfun': <staticmethod object at 0x000002323C966EB0>, 'cm': <classmethod object at 0x000002323C966EE0>, 'properfun': <property object at 0x000002323C95CD60>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None} -----------實例化對象后可以調(diào)用的類容-------------------------- 學生 lee 18 學生在吃飯 這是一個靜態(tài)方法 這是一個類方法 properfun:我的特性,是一個學生,勸你好自為之 {'name': 'lee', 'age': 18} '''
以上是“python面向?qū)ο笾泻瘮?shù)有什么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享名稱:python面向?qū)ο笾泻瘮?shù)有什么用
網(wǎng)站URL:http://m.newbst.com/article14/gohjde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應網(wǎng)站、虛擬主機、App開發(fā)、微信公眾號、外貿(mào)建站、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)