溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python面向?qū)ο蠛皖惖氖纠治?/h1>
發(fā)布時(shí)間:2022-03-04 10:24:39 來源:億速云 閱讀:155 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“Python面向?qū)ο蠛皖惖氖纠治觥?,?nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Python面向?qū)ο蠛皖惖氖纠治觥边@篇文章吧。

一、兩大編程思想

Python面向?qū)ο蠛皖惖氖纠治?></p><h3>二、類與對(duì)象</h3><p><img src=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)建

Python面向?qū)ο蠛皖惖氖纠治?></p><p><strong>舉例1:</strong></p><pre class=#實(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)方法

Python面向?qū)ο蠛皖惖氖纠治?></p><p><strong>舉例1:類屬性</strong></p><pre class=#類屬性 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)語言,在創(chuàng)建對(duì)象之后,可以動(dòng)態(tài)的綁定屬性和方法

Python面向?qū)ο蠛皖惖氖纠治?></p><p><strong>舉例:屬性綁定</strong></p><pre class=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訪問其沒有的屬性時(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)存分析:

Python面向?qū)ο蠛皖惖氖纠治?></p><h3>七、面向?qū)ο蟮娜筇卣?/h3><p><img src=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)部可以使用,在外面訪問是會(huì)報(bào)錯(cuò) #若要使用__標(biāo)識(shí)的屬性,可以先用dir()查出屬性,再訪問 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、繼承(與其他語言不同,python支持多繼承)

Python面向?qū)ο蠛皖惖氖纠治?></p><p><img src=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

八、方法重寫

Python面向?qū)ο蠛皖惖氖纠治?></p><p>舉例:</p><pre class=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é)果為:

Python面向?qū)ο蠛皖惖氖纠治?></p><p class=以上是“Python面向?qū)ο蠛皖惖氖纠治觥边@篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI