python實(shí)例化對象的具體方法
python中同樣使用關(guān)鍵字class創(chuàng)建一個類,類名稱第一個字母大寫,可以帶括號也可以不帶括號;python中實(shí)例化類不需要使用關(guān)鍵字new(也沒有這個關(guān)鍵字),類的實(shí)例化類似函數(shù)調(diào)用方式;
# coding: utf-8# 創(chuàng)建一個類,類名稱第一個字母大寫,可以帶括號也可以不帶括號 class Student(): student_count = 0 def __init__(self, name, salary):self.name = nameself.age = salaryStudent.student_count += 1 def display_count(self):print(’Total student {}’.format(Student.student_count)) def display_student(self):print(’Name: {}, age: {}’.format(self.name,self.age)) def get_class(self):if self.age >= 7 and self.age < 8: return 1if self.age >= 8 and self.age < 9: return 2if self.age >= 9 and self.age < 10: return 3if self.age >= 10 and self.age < 11: return 4else: return 0
創(chuàng)建類的對象(實(shí)例化類)
python中實(shí)例化類不需要使用關(guān)鍵字new(也沒有這個關(guān)鍵字),類的實(shí)例化類似函數(shù)調(diào)用方式。
student1 = Student(’cuiyongyuan’,10)student2 = Student(’yuanli’, 10) student1.display_student()student2.display_student() student1_class = student1.get_class()student2_class = student2.get_class()
實(shí)例擴(kuò)展:
實(shí)例化過程:
class luffy_stu: def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex def eat(self): passif __name__=='__main__': stu1 = luffy_stu(’bao’,21,’male’) #實(shí)例化過程: #1. 是先產(chǎn)生一個stu1對象, #2. luffy_stu.__init__(’stu1’,’bao’,21,’male’)再將stu1對象傳入__init__構(gòu)造函數(shù)中實(shí)例化對象
以上就是python實(shí)例化對象的具體方法的詳細(xì)內(nèi)容,更多關(guān)于python如何實(shí)例化對象的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 詳解瀏覽器的緩存機(jī)制2. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程3. Xml簡介_動力節(jié)點(diǎn)Java學(xué)院整理4. jsp文件下載功能實(shí)現(xiàn)代碼5. ajax請求后臺得到j(luò)son數(shù)據(jù)后動態(tài)生成樹形下拉框的方法6. python多線程和多進(jìn)程關(guān)系詳解7. 存儲于xml中需要的HTML轉(zhuǎn)義代碼8. Python 實(shí)現(xiàn)勞拉游戲的實(shí)例代碼(四連環(huán)、重力四子棋)9. JSP之表單提交get和post的區(qū)別詳解及實(shí)例10. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗分享
