python繼續(xù)找對(duì)象詳解
面向?qū)ο笕筇卣鳎悍庋b、繼承、多態(tài)
1、封裝(提高程序的安全性)
class Car: def __init__(self,brand): self.brand=brand def start(self): print('自行車(chē)已被蹬跑') car=Car('自行車(chē)') car.start() print(car.brand)
運(yùn)行結(jié)果
自行車(chē)已被蹬跑
自行車(chē)
一開(kāi)始它報(bào)錯(cuò)說(shuō)沒(méi)有定義name,我找老大一會(huì)不知道哪錯(cuò)了,原來(lái)是第六行
self.name
,那個(gè)時(shí)候?qū)懗?了。
看在stu里邊有哪些方法?就這樣寫(xiě)
在類的外部可以通過(guò)_Student(類名)_ _age(不希望被訪問(wèn)的)進(jìn)行訪問(wèn)
class Student: def __init__(self,name,age): self.name=name self.__age=age #年齡不希望在類的外部使用,所以加了兩個(gè)_ def show(self): print(self.name,self.__age) stu=Student('張三',20) stu.show() #在類的外部使用name和age print(stu.name) print(dir(stu)) print(stu._Student__age)
張三 20 張三 ['_Student__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__', 'name', 'show'] 20
2、繼承(提高代碼的復(fù)用性)
class Person(object): def __init__(self,name,age): self.name=name self.age=age def info(self): print(self.name,self.age) class Student(Person): def __init__(self,name,age,stu_no): super().__init__(name,age) self.stu_no=stu_no class Teacher(Person): def __init__(self,name,age,teacherofyear): super(Teacher, self).__init__(name,age) self.teacherofyear=teacherofyear stu=Student('張三',20,'1001') teacher=Teacher('李四',34,10) stu.info() teacher.info()
張三 20
李四 34
3、方法重寫(xiě)
此時(shí)只能輸出學(xué)號(hào),不滿足需求
class Person(object): def __init__(self,name,age): self.name=name self.age=age def info(self): print(self.name,self.age) class Student(Person): def __init__(self,name,age,stu_no): super().__init__(name,age) self.stu_no=stu_no def info(self): print(self.stu_no) class Teacher(Person): def __init__(self,name,age,teacherofyear): super(Teacher, self).__init__(name,age) self.teacherofyear=teacherofyear stu=Student('張三',20,'1001') teacher=Teacher('李四',34,10) stu.info() teacher.info()
1001
李四 34
看下邊的重載
class Person(object): def __init__(self,name,age): self.name=name self.age=age def info(self): print(self.name,self.age) class Student(Person): def __init__(self,name,age,stu_no): super().__init__(name,age) self.stu_no=stu_no def info(self): super(Student, self).info() print(self.stu_no) class Teacher(Person): def __init__(self,name,age,teacherofyear): super(Teacher, self).__init__(name,age) self.teacherofyear=teacherofyear stu=Student('張三',20,'1001') teacher=Teacher('李四',34,10) stu.info() print('----------------------------') teacher.info()
運(yùn)行結(jié)果
張三 20
1001
----------------------------
李四 34
把教齡輸出
class Person(object): def __init__(self,name,age): self.name=name self.age=age def info(self): print(self.name,self.age) class Student(Person): def __init__(self,name,age,stu_no): super().__init__(name,age) self.stu_no=stu_no def info(self): super(Student, self).info() print(self.stu_no) class Teacher(Person): def __init__(self,name,age,teachofyear): super().__init__(name,age) self.teachofyear=teachofyear def info(self): super().info() print('教齡',self.teachofyear) stu=Student('張三',20,'1001') teacher=Teacher('李四',34,10) stu.info() print('----------------------------') teacher.info()
運(yùn)行結(jié)果
張三 20
1001
----------------------------
李四 34
教齡 10
4、object類
5、多態(tài)(提高程序的可拓展性和可維護(hù)性)
Java就是靜態(tài)語(yǔ)言,python就是動(dòng)態(tài)語(yǔ)言
6、特殊方法和特殊屬性 特殊方法
兩個(gè)特殊的方法----創(chuàng)建
1初始化init
2new
特殊屬性
兩個(gè)下劃線開(kāi)始,兩個(gè)下劃線結(jié)束就是特殊屬性
綁定兩個(gè)屬性
class A: pass class B: pass class C(A,B): def __init__(self,name,age): self.name=name self.age=age #創(chuàng)建C類的對(duì)象 x=C('Jack',20)#x是C類的一個(gè)實(shí)例對(duì)象 print(x.__dict__)
{'name': 'Jack', 'age': 20}
pycharm使用的小發(fā)現(xiàn)
點(diǎn)擊加號(hào)那里,就會(huì)釋放,點(diǎn)擊減號(hào)就會(huì)縮成這樣,這說(shuō)明了被縮起來(lái)的內(nèi)容都是隸屬于這個(gè)類的。
看最左側(cè)出現(xiàn)了箭頭,他的意思是重寫(xiě)person類中的方法
英文
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Python 執(zhí)行字符串表達(dá)式函數(shù)(eval exec execfile)
今天在網(wǎng)上搜尋一些應(yīng)用的例子時(shí),發(fā)現(xiàn)有人用TK僅僅幾行代碼就寫(xiě)了個(gè)簡(jiǎn)易的計(jì)算器,驚為天人?;貞浧饎倢W(xué)軟件技術(shù)基礎(chǔ)時(shí)編寫(xiě)簡(jiǎn)易計(jì)算器的艱辛,頓時(shí)淚流滿面2014-08-08基于python3 pyQt5 QtDesignner實(shí)現(xiàn)窗口化猜數(shù)字游戲功能
這篇文章主要介紹了基于python3 pyQt5 QtDesignner實(shí)現(xiàn)窗口化猜數(shù)字游戲功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07python實(shí)現(xiàn)mask矩陣示例(根據(jù)列表所給元素)
這篇文章主要介紹了python實(shí)現(xiàn)mask矩陣示例(根據(jù)列表所給元素),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Python3 assert斷言實(shí)現(xiàn)原理解析
這篇文章主要介紹了Python3 assert斷言實(shí)現(xiàn)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03解決pytorch 的state_dict()拷貝問(wèn)題
這篇文章主要介紹了解決pytorch 的state_dict()拷貝問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03python調(diào)用文件時(shí)找不到相對(duì)路徑的解決方案
這篇文章主要介紹了python調(diào)用文件時(shí)找不到相對(duì)路徑的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03在python中求分布函數(shù)相關(guān)的包實(shí)例
這篇文章主要介紹了在python中求分布函數(shù)相關(guān)的包實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04