Python入門之面向?qū)ο蠛皖?/h1>
更新時(shí)間:2022年01月10日 11:43:39 作者:Code小白
這篇文章主要為大家介紹了Python面向?qū)ο蠛皖?,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
一、兩大編程思想

二、類與對(duì)象

簡(jiǎn)單舉例:

python中一切皆對(duì)象,開局一張圖:

三、定義Python中的類

舉例1:
class Student:
pass
print(id(Student)) #1149225945800
print(type(Student)) #<class 'type'>
print(Student) #<class '__main__.Student'>
舉例2:
class Student:
native_place='吉林' #類屬性
def __init__(self,name,age):
self.name=name
self.age=age
#實(shí)例方法
def eat(self):
print("學(xué)生在吃飯")
#靜態(tài)方法
@staticmethod
def method():
print("我是靜態(tài)方法")
#類方法
@classmethod
def cm(cls):
print("我是類方法")
四、對(duì)象創(chuàng)建

舉例1:
#實(shí)例對(duì)象
student1=Student("張三",18)
print(student1)
print(id(student1))
print(type(student1))
print("-------------------------------------------")
#類對(duì)象,代表所在的類
print(Student)
print(id(Student))
print(type(Student))
舉例2:
#實(shí)例對(duì)象
student1=Student("張三",18)
print(student1.name)
print(student1.age)
#實(shí)例方法調(diào)用有以下兩種使用:
print(student1.eat())
print(Student.eat(student1))
五、類屬性、類方法、靜態(tài)方法

舉例1:類屬性
#類屬性
student1=Student("張三",18)
student2=Student("李四",19)
print(Student.native_place) #吉林
print(student1.native_place)#吉林
print(student2.native_place)#吉林
Student.native_place='四川'
print(student1.native_place)#四川
print(student2.native_place)#四川
#---------------------------------------------------------
student1.native_place='廣東'
print(student1.native_place)#廣東
print(student2.native_place)#四川
舉例2:類方法、靜態(tài)方法
#類方法、靜態(tài)方法使用
student1=Student("張三",18)
Student.method()#我是靜態(tài)方法
Student.cm()#我是類方法
六、動(dòng)態(tài)綁定屬性和方法
Python是動(dòng)態(tài)語(yǔ)言,在創(chuàng)建對(duì)象之后,可以動(dòng)態(tài)的綁定屬性和方法

舉例:屬性綁定
class Student:
def __init__(self,name,age):
self.name=name
self.age=age
#實(shí)例方法
def eat(self):
print("學(xué)生在吃飯")
student1=Student('張三',19)
student2=Student('李四',20)
print(id(student1)) #2363920157896
print(id(student2)) #2363920157960
print("--------綁定屬性-------")
print("綁定屬性-----為student2動(dòng)態(tài)的綁定gender屬性-------")
student2.gender='男'
print(student1.name,student1.age) #張三 19
#print(student1.gender) 當(dāng)student1訪問(wèn)其沒(méi)有的屬性時(shí),會(huì)報(bào)錯(cuò)AttributeError: 'Student' object has no attribute 'gender'
print(student2.name,student2.age,student2.gender) #李四 20 男
print("--------綁定方法-------")
def show():
print('我是show方法')
student1.show=show
student1.show() #我是show方法
student2.show() #報(bào)錯(cuò)AttributeError: 'Student' object has no attribute 'show'
內(nèi)存分析:

七、面向?qū)ο蟮娜筇卣?/h2>

1、封裝
class Car:
def __init__(self,brand,age):
self.brand=brand
self.__age=age
def show(self):
print(self.brand,self.__age)
car1=Car('寶馬X5',50)
print(car1.brand) #寶馬X5
# print(car1.__age) __標(biāo)識(shí)的屬性限制其在類外使用,在類的內(nèi)部可以使用,在外面訪問(wèn)是會(huì)報(bào)錯(cuò)
#若要使用__標(biāo)識(shí)的屬性,可以先用dir()查出屬性,再訪問(wèn)
print(dir(car1))
#輸出['_Car__age', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'brand', 'show']
print(car1._Car__age)#50
2、繼承(與其他語(yǔ)言不同,python支持多繼承)


舉例:
class People:
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
class Student(People):
def __init__(self,name,age,sno):
super().__init__(name,age)
self.sno=sno
class Teacher(People):
def __init__(self,name,age,teachofage):
super().__init__(name,age)
self.teachofage=teachofage
student1=Student('張三',18,122)
teacher1=Teacher('李四',36,10)
student1.info() #張三 18
teacher1.info() #李四 36
八、方法重寫

舉例:
class People:
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
class Student(People):
def __init__(self,name,age,sno):
super().__init__(name,age)
self.sno=sno
def info(self):
super().info()
print(self.sno)
class Teacher(People):
def __init__(self,name,age,teachofage):
super().__init__(name,age)
self.teachofage=teachofage
def info(self):
super().info()
print(self.teachofage)
student1=Student('張三',18,122)
teacher1=Teacher('李四',36,10)
student1.info()
teacher1.info()
結(jié)果為:

總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
-
python數(shù)據(jù)結(jié)構(gòu)之圖深度優(yōu)先和廣度優(yōu)先實(shí)例詳解
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)之圖深度優(yōu)先和廣度優(yōu)先,較為詳細(xì)的分析了深度優(yōu)先和廣度優(yōu)先算法的概念與原理,并給出了完整實(shí)現(xiàn)算法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下 2015-07-07
-
Python3-異步進(jìn)程回調(diào)函數(shù)(callback())介紹
這篇文章主要介紹了Python3-異步進(jìn)程回調(diào)函數(shù)(callback())介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧 2020-05-05
-
Python爬取你好李煥英豆瓣短評(píng)生成詞云的示例代碼
這篇文章主要介紹了Python爬取你好李煥英豆瓣短評(píng)生成詞云,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下 2021-02-02
-
Python使用scrapy采集數(shù)據(jù)時(shí)為每個(gè)請(qǐng)求隨機(jī)分配user-agent的方法
這篇文章主要介紹了Python使用scrapy采集數(shù)據(jù)時(shí)為每個(gè)請(qǐng)求隨機(jī)分配user-agent的方法,涉及Python使用scrapy采集數(shù)據(jù)的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下 2015-04-04
-
python數(shù)字圖像處理之基本形態(tài)學(xué)濾波
這篇文章主要為大家介紹了python數(shù)字圖像處理之基本形態(tài)學(xué)濾波示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪 2022-06-06
-
詳解Python中數(shù)據(jù)庫(kù)管理模塊shelve和dbm的應(yīng)用
作為常用的 python 自帶數(shù)據(jù)庫(kù)管理模塊,shelve 和 dbm 都是非常方便的對(duì)象持久化存儲(chǔ)和檢索工具,本文將從用法、優(yōu)勢(shì)以及不同點(diǎn)等方面進(jìn)行介紹,希望對(duì)大家有所幫助 2023-10-10
-
Python數(shù)據(jù)序列化技術(shù)總結(jié)
在現(xiàn)代軟件開發(fā)中,數(shù)據(jù)序列化是一個(gè)關(guān)鍵環(huán)節(jié),它允許我們將復(fù)雜的數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為可存儲(chǔ)或可傳輸?shù)母袷?,Python提供了多種數(shù)據(jù)序列化技術(shù),每種技術(shù)都有其獨(dú)特的性能優(yōu)勢(shì)和適用場(chǎng)景,本文將詳細(xì)介紹幾種強(qiáng)大的Python數(shù)據(jù)序列化技術(shù),需要的朋友可以參考下 2025-03-03
最新評(píng)論
一、兩大編程思想

二、類與對(duì)象

簡(jiǎn)單舉例:

python中一切皆對(duì)象,開局一張圖:

三、定義Python中的類

舉例1:
class Student:
pass
print(id(Student)) #1149225945800
print(type(Student)) #<class 'type'>
print(Student) #<class '__main__.Student'>
舉例2:
class Student:
native_place='吉林' #類屬性
def __init__(self,name,age):
self.name=name
self.age=age
#實(shí)例方法
def eat(self):
print("學(xué)生在吃飯")
#靜態(tài)方法
@staticmethod
def method():
print("我是靜態(tài)方法")
#類方法
@classmethod
def cm(cls):
print("我是類方法")
四、對(duì)象創(chuàng)建

舉例1:
#實(shí)例對(duì)象
student1=Student("張三",18)
print(student1)
print(id(student1))
print(type(student1))
print("-------------------------------------------")
#類對(duì)象,代表所在的類
print(Student)
print(id(Student))
print(type(Student))
舉例2:
#實(shí)例對(duì)象
student1=Student("張三",18)
print(student1.name)
print(student1.age)
#實(shí)例方法調(diào)用有以下兩種使用:
print(student1.eat())
print(Student.eat(student1))
五、類屬性、類方法、靜態(tài)方法

舉例1:類屬性
#類屬性
student1=Student("張三",18)
student2=Student("李四",19)
print(Student.native_place) #吉林
print(student1.native_place)#吉林
print(student2.native_place)#吉林
Student.native_place='四川'
print(student1.native_place)#四川
print(student2.native_place)#四川
#---------------------------------------------------------
student1.native_place='廣東'
print(student1.native_place)#廣東
print(student2.native_place)#四川
舉例2:類方法、靜態(tài)方法
#類方法、靜態(tài)方法使用
student1=Student("張三",18)
Student.method()#我是靜態(tài)方法
Student.cm()#我是類方法
六、動(dòng)態(tài)綁定屬性和方法
Python是動(dòng)態(tài)語(yǔ)言,在創(chuàng)建對(duì)象之后,可以動(dòng)態(tài)的綁定屬性和方法

舉例:屬性綁定
class Student:
def __init__(self,name,age):
self.name=name
self.age=age
#實(shí)例方法
def eat(self):
print("學(xué)生在吃飯")
student1=Student('張三',19)
student2=Student('李四',20)
print(id(student1)) #2363920157896
print(id(student2)) #2363920157960
print("--------綁定屬性-------")
print("綁定屬性-----為student2動(dòng)態(tài)的綁定gender屬性-------")
student2.gender='男'
print(student1.name,student1.age) #張三 19
#print(student1.gender) 當(dāng)student1訪問(wèn)其沒(méi)有的屬性時(shí),會(huì)報(bào)錯(cuò)AttributeError: 'Student' object has no attribute 'gender'
print(student2.name,student2.age,student2.gender) #李四 20 男
print("--------綁定方法-------")
def show():
print('我是show方法')
student1.show=show
student1.show() #我是show方法
student2.show() #報(bào)錯(cuò)AttributeError: 'Student' object has no attribute 'show'
內(nèi)存分析:

七、面向?qū)ο蟮娜筇卣?/h2>

1、封裝
class Car:
def __init__(self,brand,age):
self.brand=brand
self.__age=age
def show(self):
print(self.brand,self.__age)
car1=Car('寶馬X5',50)
print(car1.brand) #寶馬X5
# print(car1.__age) __標(biāo)識(shí)的屬性限制其在類外使用,在類的內(nèi)部可以使用,在外面訪問(wèn)是會(huì)報(bào)錯(cuò)
#若要使用__標(biāo)識(shí)的屬性,可以先用dir()查出屬性,再訪問(wèn)
print(dir(car1))
#輸出['_Car__age', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'brand', 'show']
print(car1._Car__age)#50
2、繼承(與其他語(yǔ)言不同,python支持多繼承)


舉例:
class People:
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
class Student(People):
def __init__(self,name,age,sno):
super().__init__(name,age)
self.sno=sno
class Teacher(People):
def __init__(self,name,age,teachofage):
super().__init__(name,age)
self.teachofage=teachofage
student1=Student('張三',18,122)
teacher1=Teacher('李四',36,10)
student1.info() #張三 18
teacher1.info() #李四 36
八、方法重寫

舉例:
class People:
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
class Student(People):
def __init__(self,name,age,sno):
super().__init__(name,age)
self.sno=sno
def info(self):
super().info()
print(self.sno)
class Teacher(People):
def __init__(self,name,age,teachofage):
super().__init__(name,age)
self.teachofage=teachofage
def info(self):
super().info()
print(self.teachofage)
student1=Student('張三',18,122)
teacher1=Teacher('李四',36,10)
student1.info()
teacher1.info()
結(jié)果為:

總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
python數(shù)據(jù)結(jié)構(gòu)之圖深度優(yōu)先和廣度優(yōu)先實(shí)例詳解
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)之圖深度優(yōu)先和廣度優(yōu)先,較為詳細(xì)的分析了深度優(yōu)先和廣度優(yōu)先算法的概念與原理,并給出了完整實(shí)現(xiàn)算法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
Python3-異步進(jìn)程回調(diào)函數(shù)(callback())介紹
這篇文章主要介紹了Python3-異步進(jìn)程回調(diào)函數(shù)(callback())介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
Python爬取你好李煥英豆瓣短評(píng)生成詞云的示例代碼
這篇文章主要介紹了Python爬取你好李煥英豆瓣短評(píng)生成詞云,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Python使用scrapy采集數(shù)據(jù)時(shí)為每個(gè)請(qǐng)求隨機(jī)分配user-agent的方法
這篇文章主要介紹了Python使用scrapy采集數(shù)據(jù)時(shí)為每個(gè)請(qǐng)求隨機(jī)分配user-agent的方法,涉及Python使用scrapy采集數(shù)據(jù)的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
python數(shù)字圖像處理之基本形態(tài)學(xué)濾波
這篇文章主要為大家介紹了python數(shù)字圖像處理之基本形態(tài)學(xué)濾波示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
詳解Python中數(shù)據(jù)庫(kù)管理模塊shelve和dbm的應(yīng)用
作為常用的 python 自帶數(shù)據(jù)庫(kù)管理模塊,shelve 和 dbm 都是非常方便的對(duì)象持久化存儲(chǔ)和檢索工具,本文將從用法、優(yōu)勢(shì)以及不同點(diǎn)等方面進(jìn)行介紹,希望對(duì)大家有所幫助2023-10-10
Python數(shù)據(jù)序列化技術(shù)總結(jié)
在現(xiàn)代軟件開發(fā)中,數(shù)據(jù)序列化是一個(gè)關(guān)鍵環(huán)節(jié),它允許我們將復(fù)雜的數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為可存儲(chǔ)或可傳輸?shù)母袷?,Python提供了多種數(shù)據(jù)序列化技術(shù),每種技術(shù)都有其獨(dú)特的性能優(yōu)勢(shì)和適用場(chǎng)景,本文將詳細(xì)介紹幾種強(qiáng)大的Python數(shù)據(jù)序列化技術(shù),需要的朋友可以參考下2025-03-03

