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