【Python基础知识-pycharm版】第九节_面向对象的三大特征
生活随笔
收集整理的這篇文章主要介紹了
【Python基础知识-pycharm版】第九节_面向对象的三大特征
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第九節
- 方法
- 方法沒有重載
- 私有屬性和私有方法(實現封裝)
- @property裝飾器_get和set方法
- 面向對象的三大特征說明(封裝、繼承、多態)
- 繼承
- 方法的重寫(類成員的繼承和重寫)
- 查看類的繼承結構
- object根類_dir() 查看對象屬性
- 重寫__str__()方法
- 多重繼承
- mro()
- super()獲得父類的定義
- 多態
- 特殊方法和運算符重載
- 特殊屬性
- 對象的淺拷貝和深拷貝
- 組合
- 設計模式_工廠模式
- 設計模式_單例模式
方法
方法沒有重載
私有屬性和私有方法(實現封裝)
#測試私有屬性 class Employee:def __init__(self,name,age):self.name=nameself.__age=agee=Employee('高琪',18)print(e.name) #print(e.age) print(e._Employee__age) print(dir(e))輸出:
高琪 18 ['_Employee__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']Process finished with exit code 0 #測試私有屬性 class Employee:__company='程序員'def __init__(self,name,age):self.name=nameself.__age=age #私有屬性def __work(self): #私有方法print('好好工作!')print('年齡:{0}'.format(self.__age))e=Employee('高琪',18)print(e.name) print(e._Employee__age) print(dir(e)) e._Employee__work() print(Employee._Employee__company)輸出:
高琪 18 ['_Employee__age', '_Employee__company', '_Employee__work', '__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'] 好好工作! 年齡:18 程序員@property裝飾器_get和set方法
普通調用:
結果:
salary run ...Process finished with exit code 0采用@property
class Employee:@propertydef salary(self):print('salary run ...')return 10000 emp1=Employee() #emp1.salary() print(emp1.salary)結果:
salary run ... 10000Process finished with exit code 0相當于變成屬性的調用
但是當賦值時出現報錯
簡單測試@property
class Employee:def __init__(self,name,salary):self.name = nameself.salary = salaryemp1=Employee('高琪',30000) print(emp1.salary) emp1.salary=20000 print(emp1.salary) 30000 20000這個時候沒問題 簡單的讀取,但是封裝化要求用戶在使用的時候要完善,不能當輸入-2000時仍然輸出-2000,需要有提示的細節
首先是不使用@的
加入@property
#簡單測試@property class Employee:def __init__(self,name,salary):self.__name = nameself.__salary = salary@propertydef salary(self):return self.__salary@salary.setterdef salary(self,salary):if 1000<salary<50000:self.__salary=salaryelse:print('錄入錯誤!薪水在1000-50000這個范圍') emp1=Employee('高琪',30000) print(emp1.salary) emp1.salary=-20000 print(emp1.salary) 30000 錄入錯誤!薪水在1000-50000這個范圍 30000面向對象的三大特征說明(封裝、繼承、多態)
繼承
方法的重寫(類成員的繼承和重寫)
class Person:def __init__(self,name,age):self.name=nameself.__age=agedef say_age(self):print('我的年齡是:',self.__age)def say_introduce(self):print('我的名字是{0}'.format(self.name))class Student(Person):def __init__(self, name, age, score):Person.__init__(self, name, age) # 必須顯式的調用父類初始化方法,不然解釋器不會去調用self.score = scoredef say_introduce(self):'''重寫了父類的方法'''print('報告老師,我的名字是{0}'.format(self.name))s=Student('高琪',18,60) s.say_age() s.say_introduce() 我的年齡是: 18 報告老師,我的名字是高琪Process finished with exit code 0重寫是對父類的方法進行修改
查看類的繼承結構
object根類_dir() 查看對象屬性
重寫__str__()方法
#測試重寫object的__str__() class Person:def __init__(self,name):self.name=namedef __str__(self):return '名字是:{0}'.format(self.name)p=Person('高琪') print(p) 名字是:高琪Process finished with exit code 0多重繼承
mro()
super()獲得父類的定義
多態
特殊方法和運算符重載
特殊屬性
對象的淺拷貝和深拷貝
組合
設計模式_工廠模式
設計模式_單例模式
總結
以上是生活随笔為你收集整理的【Python基础知识-pycharm版】第九节_面向对象的三大特征的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: flash html5 chrome,为
- 下一篇: python中importlib模块安装