日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【Python基础知识-pycharm版】第九节_面向对象的三大特征

發布時間:2024/7/5 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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方法


普通調用:

class Employee:def salary(self):print('salary run ...')return 10000 emp1=Employee() emp1.salary()

結果:

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

相當于變成屬性的調用
但是當賦值時出現報錯

class Employee:@propertydef salary(self):print('salary run ...')return 10000 emp1=Employee() #emp1.salary() print(emp1.salary) emp1.salary=30000 salary run ... 10000 Traceback (most recent call last):File "D:/PycharmProjects/MyTest/Day_0722/mytest05.py", line 18, in <module>emp1.salary=30000 AttributeError: can't set attributeProcess finished with exit code 1

簡單測試@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 class Employee:def __init__(self,name,salary):self.__name = nameself.__salary = salarydef get_salary(self):return self.__salarydef set_salary(self,salary):if 1000<salary<50000:self.__salary=salaryelse:print('錄入錯誤!薪水在1000-50000這個范圍') emp1=Employee('高琪',30000) print(emp1.get_salary()) emp1.set_salary(-20000) print(emp1.get_salary()) 30000 錄入錯誤!薪水在1000-50000這個范圍 30000 Process finished with exit code 0 #簡單測試@property class Employee:def __init__(self,name,salary):self.__name = nameself.__salary = salarydef get_salary(self):return self.__salarydef set_salary(self,salary):if 1000<salary<50000:self.__salary=salaryelse:print('錄入錯誤!薪水在1000-50000這個范圍') emp1=Employee('高琪',30000) print(emp1.get_salary()) emp1.set_salary(20000) print(emp1.get_salary()) 30000 20000

加入@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('不知道') class Student(Person):def __init__(self,name,age,score):Person.__init__(self,name,age)self.score=score print(Student.mro()) s=Student('高琪',18,60) s.say_age() print(s.name) [<class '__main__.Student'>, <class '__main__.Person'>, <class 'object'>] 不知道 高琪 #測試繼承的基本使用 class Person:def __init__(self,name,age):self.name=nameself.__age=age #私有屬性def say_age(self):print('不知道') class Student(Person):def __init__(self,name,age,score):Person.__init__(self,name,age) #必須顯式的調用父類初始化方法,不然解釋器不會去調用self.score=score print(Student.mro()) s=Student('高琪',18,60) s.say_age() print(s.name) print(dir(s)) print(s._Person__age) [<class '__main__.Student'>, <class '__main__.Person'>, <class 'object'>] 不知道 高琪 ['_Person__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', 'say_age', 'score'] 18

方法的重寫(類成員的繼承和重寫)

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()


#多重繼承 class A:def aa(self):print('aa')def say(self):print('say AA')class B:def bb(self):print('bb')def say(self):print('say BB')class C(B,A):def cc(self):print('cc')c=C() print(C.mro()) #打印類的層次結構 c.say() #解釋器尋找的方法是從左到右的方式尋找,此時會執行B類中的say() [<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>] say BBProcess finished with exit code 0

super()獲得父類的定義


多態



特殊方法和運算符重載



#測試運算符的重載 class Person:def __init__(self,name):self.name=namedef __add__(self,other):if isinstance(self,Person):return'{0}--{1}'.format(self.name,other.name)else:return'不是同類對象,不能相加'def __mul__(self,other):if isinstance(other,int):return self.name*otherelse:return'不是同類對象,不能相加'p1=Person('高琪') p2=Person('高嘻嘻') x = p1+p2 print(x) print(p1*3) 高琪--高嘻嘻 高琪高琪高琪Process finished with exit code 0

特殊屬性

對象的淺拷貝和深拷貝




組合



設計模式_工廠模式



設計模式_單例模式







總結

以上是生活随笔為你收集整理的【Python基础知识-pycharm版】第九节_面向对象的三大特征的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。