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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python面向对象——面向对象介绍、实现面向对象编程、定义类、再调用类产生对象、总结__init__方法、查找顺序

發(fā)布時間:2024/3/13 python 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python面向对象——面向对象介绍、实现面向对象编程、定义类、再调用类产生对象、总结__init__方法、查找顺序 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 面向?qū)ο蠼榻B
  • 實現(xiàn)面向?qū)ο缶幊?/li>
    • 一:先定義類
    • 二:再調(diào)用類產(chǎn)生對象
    • 總結(jié)__init__方法
  • 查找順序

面向?qū)ο蠼榻B

''' 面向過程:核心是"過程"二字過程的終極奧義就是將程序流程化過程是"流水線",用來分步驟解決問題的面向?qū)ο?#xff1a;核心是"對象"二字對象的終極奧義就是將程序"整合"對象是"容器",用來盛放數(shù)據(jù)與功能的類也是"容器",該容器用來存放同類對象共有的數(shù)據(jù)與功能python這門語言到底提供了什么語法來允許我們將數(shù)據(jù)與功能很好地整合好一起呢??? ''' # 程序=數(shù)據(jù)+功能# 學(xué)生的容器=學(xué)生的數(shù)據(jù)+學(xué)生的功能 # 課程的容器=課程的數(shù)據(jù)+課程的功能# 粉撲、眼影、各種顏料=》原材料=》數(shù)據(jù) # 眉筆、小刷子 =》工具 =》功能 # 學(xué)生的功能 def tell_stu_info(stu_obj):print('學(xué)生信息:名字:%s 年齡:%s 性別:%s' %(stu_obj['stu_name'],stu_obj['stu_age'],stu_obj['stu_gender']))def set_info(stu_obj,x,y,z):stu_obj['stu_name']=xstu_obj['stu_age']=ystu_obj['stu_gender']=zstu_obj={'stu_school':'oldboy','stu_name':'egon','stu_age':18,'stu_gender':'male','tell_stu_info': tell_stu_info,'set_info':set_info }stu1_obj={'stu_school':'oldboy','stu_name':'lili','stu_age':19,'stu_gender':'female','tell_stu_info': tell_stu_info,'set_info':set_info }# 課程的數(shù)據(jù) course_name='python' course_period='6mons' course_score=10# 課程的功能 def tell_coure_info():print('課程信息:名字:%s 周期:%s 學(xué)分:%s' %(course_name,course_period,course_score))

對比與面向過程

# 學(xué)生的數(shù)據(jù) stu_name='egon' stu_age=18 stu_gender='male'# 學(xué)生的功能 def tell_stu_info():print('學(xué)生信息:名字:%s 年齡:%s 性別:%s' %(stu_name,stu_age,stu_gender))def set_info():stu_name='EGON'stu_age=19stu_gender='female'# 課程的數(shù)據(jù) course_name='python' course_period='6mons' course_score=10# 課程的功能 def tell_coure_info():print('課程信息:名字:%s 周期:%s 學(xué)分:%s' %(course_name,course_period,course_score))

實現(xiàn)面向?qū)ο缶幊?/h1>

一:先定義類

類是對象相似數(shù)據(jù)與功能的集合體 所以類體中最常見的是變量與函數(shù)的定義,但是類體其實是可以包含任意其他代碼的 注意:類體代碼是在類定義階段就會立即執(zhí)行,會產(chǎn)生類的名稱空間

class Student:# 1、變量的定義stu_school='oldboy'# 2、功能的定義def tell_stu_info(stu_obj):print('學(xué)生信息:名字:%s 年齡:%s 性別:%s' %(stu_obj['stu_name'],stu_obj['stu_age'],stu_obj['stu_gender']))def set_info(stu_obj,x,y,z):stu_obj['stu_name']=xstu_obj['stu_age']=ystu_obj['stu_gender']=zprint(Student.__dict__)

屬性訪問的語法 1、訪問數(shù)據(jù)屬性

print(Student.stu_school) # Student.__dict__['stu_school']

2、訪問函數(shù)屬性

print(Student.set_info) # Student.__dict__['set_info']Student.x=1111 #Student.__dict__['x]=111 print(Student.__dict__)

二:再調(diào)用類產(chǎn)生對象

stu1_obj=Student() stu2_obj=Student() stu3_obj=Student()print(stu1_obj.__dict__) print(stu2_obj.__dict__) print(stu3_obj.__dict__)

為對象定制自己獨有的屬性 問題1:代碼重復(fù) 問題2:屬性的查找順序

stu1_obj.stu_name='egon' # stu1_obj.__dict__['stu_name']='egon' stu1_obj.stu_age=18 # stu1_obj.__dict__['stu_age']=18 stu1_obj.stu_gender='male' # stu1_obj.__dict__['stu_gender']='male' print(stu1_obj.__dict__)stu2_obj.stu_name='lili' stu2_obj.stu_age=19 stu2_obj.stu_gender='female' print(stu2_obj.__dict__)stu3_obj.stu_name='jack' stu3_obj.stu_age=20 stu3_obj.stu_gender='male' print(stu2_obj.__dict__)

解決方案

# 解決方法一 def init(obj,x,y,z):obj.stu_name=xobj.stu_age=yobj.stu_gender=zinit(stu1_obj,'egon',18,'male') init(stu2_obj,'lili',19,'female') init(stu2_obj,'jack',20,'male')# 解決方案二: # 一:先定義類 class Student:# 1、變量的定義stu_school='oldboy'# 空對象,'egon',18,'male'def __init__(obj,x,y,z):obj.stu_name=x # 空對象.stu_name='egon'obj.stu_age=y # 空對象.stu_age=18obj.stu_gender=z # 空對象.stu_gender='male'# return None# 2、功能的定義def tell_stu_info(stu_obj):print('學(xué)生信息:名字:%s 年齡:%s 性別:%s' %(stu_obj['stu_name'],stu_obj['stu_age'],stu_obj['stu_gender']))def set_info(stu_obj,x,y,z):stu_obj['stu_name']=xstu_obj['stu_age']=ystu_obj['stu_gender']=z# print('========>') 二:再調(diào)用類產(chǎn)生對象 調(diào)用類的過程又稱之為實例化,發(fā)生了三件事 1、先產(chǎn)生一個空對象 2、python會自動調(diào)用類中的__init__方法然將空對象已經(jīng)調(diào)用類時括號內(nèi)傳入的參數(shù)一同傳給__init__方法 3、返回初始完的對象 stu1_obj=Student('egon',18,'male') # Student.__init__(空對象,'egon',18,'male') stu2_obj=Student('lili',19,'female') stu3_obj=Student('jack',20,'male')print(stu1_obj.__dict__) print(stu2_obj.__dict__) print(stu3_obj.__dict__)

總結(jié)__init__方法

1、會在調(diào)用類時自動觸發(fā)執(zhí)行,用來為對象初始化自己獨有的數(shù)據(jù) 2、__init__內(nèi)應(yīng)該存放是為對象初始化屬性的功能,但是是可以存放任意其他代碼,想要在類調(diào)用時就立刻執(zhí)行的代碼都可以放到該方法內(nèi) 3、__init__方法必須返回None

查找順序

class Student:# 1、變量的定義stu_school = 'oldboy'count = 0# 空對象,'egon',18,'male'def __init__(self, x, y, z):Student.count += 1self.stu_name = x # 空對象.stu_name='egon'self.stu_age = y # 空對象.stu_age=18self.stu_gender = z # 空對象.stu_gender='male'# return None# 2、功能的定義def tell_stu_info(self):print('學(xué)生信息:名字:%s 年齡:%s 性別:%s' % (self.stu_name,self.stu_age,self.stu_gender))def set_info(self, x, y, z):self.stu_name = xself.stu_age = yself.stu_gender = zdef choose(self, x):print('正在選課')self.course = xstu1_obj = Student('egon', 18, 'male') # Student.__init__(空對象,'egon',18,'male') stu2_obj = Student('lili', 19, 'female') stu3_obj = Student('jack', 20, 'male')print(stu1_obj.count) print(stu2_obj.count) print(stu3_obj.count)類中存放的是對象共有的數(shù)據(jù)與功能 一:類可以訪問: 1、類的數(shù)據(jù)屬性 print(Student.stu_school) 2、類的函數(shù)屬性 print(Student.tell_stu_info) print(Student.set_info)二:但其實類中的東西是給對象用的 1、類的數(shù)據(jù)屬性是共享給所有對象用的,大家訪問的地址都一樣 print(id(Student.stu_school))print(id(stu1_obj.stu_school)) print(id(stu2_obj.stu_school)) print(id(stu3_obj.stu_school))Student.stu_school = 'OLDBOY' stu1_obj.stu_school = 'OLDBOY' print(stu1_obj.stu_school)print(Student.stu_school) print(stu2_obj.stu_school) print(stu3_obj.stu_school)2、類中定義的函數(shù)主要是給對象使用的,而且是綁定給對象的,雖然所有對象指向的都是相同的功能,但是綁定到不同的對象就是不同的綁定方法,內(nèi)存地址各不相同類調(diào)用自己的函數(shù)屬性必須嚴格按照函數(shù)的用法來 print(Student.tell_stu_info) print(Student.set_info)Student.tell_stu_info(stu1_obj) Student.tell_stu_info(stu2_obj) Student.tell_stu_info(stu3_obj)Student.set_info(stu1_obj, 'EGON', 19, 'MALE') Student.tell_stu_info(stu1_obj)綁定方法的特殊之處在于:誰來調(diào)用綁定方法就會將誰當(dāng)做第一個參數(shù)自動傳入 print(Student.tell_stu_info) print(stu1_obj.tell_stu_info) print(stu2_obj.tell_stu_info) print(stu3_obj.tell_stu_info)stu1_obj.tell_stu_info() # tell_stu_info(stu1_obj) stu2_obj.tell_stu_info() # tell_stu_info(stu2_obj) stu3_obj.tell_stu_info() # tell_stu_info(stu3_obj)stu1_obj.choose('python全棧開發(fā)') print(stu1_obj.course)stu2_obj.choose('linux運維') print(stu2_obj.course)stu3_obj.choose('高級架構(gòu)師') print(stu3_obj.course)l1 = ['aa', 'bb', 'cc'] # l=list([1,2,3]) l2 = [111, 222, 333] # l=list([1,2,3]) print(l1.append) print(list.append)l1.append('dd') l2.append('dd') print(l1) print(l2)list.append(l1, 'dd') list.append(l2, 'dd') print(l1) print(l2)

總結(jié)

以上是生活随笔為你收集整理的Python面向对象——面向对象介绍、实现面向对象编程、定义类、再调用类产生对象、总结__init__方法、查找顺序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。