python繼續(xù)找對(duì)象詳解
面向?qū)ο笕筇卣鳎悍庋b、繼承、多態(tài)

1、封裝(提高程序的安全性)
class Car:
def __init__(self,brand):
self.brand=brand
def start(self):
print('自行車已被蹬跑')
car=Car('自行車')
car.start()
print(car.brand)
運(yùn)行結(jié)果
自行車已被蹬跑
自行車

一開始它報(bào)錯(cuò)說沒有定義name,我找老大一會(huì)不知道哪錯(cuò)了,原來是第六行
self.name,那個(gè)時(shí)候?qū)懗?了。
看在stu里邊有哪些方法?就這樣寫

在類的外部可以通過_Student(類名)_ _age(不希望被訪問的)進(jì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、方法重寫

此時(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)語言,python就是動(dòng)態(tài)語言
6、特殊方法和特殊屬性 特殊方法

兩個(gè)特殊的方法----創(chuàng)建
1初始化init
2new
特殊屬性
兩個(gè)下劃線開始,兩個(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ì)縮成這樣,這說明了被縮起來的內(nèi)容都是隸屬于這個(gè)類的。

看最左側(cè)出現(xiàn)了箭頭,他的意思是重寫person類中的方法

英文

總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
pytorch:實(shí)現(xiàn)簡(jiǎn)單的GAN示例(MNIST數(shù)據(jù)集)
今天小編就為大家分享一篇pytorch:實(shí)現(xiàn)簡(jiǎn)單的GAN示例(MNIST數(shù)據(jù)集),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python-typing: 類型標(biāo)注與支持 Any類型詳解
這篇文章主要介紹了Python-typing: 類型標(biāo)注與支持 Any類型詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-05-05
Python時(shí)間獲取及轉(zhuǎn)換知識(shí)匯總
這篇文章主要介紹了Python時(shí)間獲取及轉(zhuǎn)換知識(shí)匯總的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
Python用戶推薦系統(tǒng)曼哈頓算法實(shí)現(xiàn)完整代碼
這篇文章主要介紹了Python用戶推薦系統(tǒng)曼哈頓算法實(shí)現(xiàn)完整代碼,簡(jiǎn)單介紹了曼哈頓距離的定義,然后分享了其Python實(shí)現(xiàn)代碼,具有一定借鑒價(jià)值,需要的朋友可以了解下。2017-12-12
Python實(shí)現(xiàn)獲取磁盤剩余空間的2種方法
這篇文章主要介紹了Python實(shí)現(xiàn)獲取磁盤剩余空間的2種方法,結(jié)合具體實(shí)例形式分析了Python操作計(jì)算機(jī)硬件的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06
解決anaconda安裝pytorch報(bào)錯(cuò)找不到包的問題
這篇文章主要介紹了解決anaconda安裝pytorch報(bào)錯(cuò)找不到包的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03

