python實例化對象的具體方法
python中同樣使用關(guān)鍵字class創(chuàng)建一個類,類名稱第一個字母大寫,可以帶括號也可以不帶括號;python中實例化類不需要使用關(guān)鍵字new(也沒有這個關(guān)鍵字),類的實例化類似函數(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)建類的對象(實例化類)
python中實例化類不需要使用關(guān)鍵字new(也沒有這個關(guān)鍵字),類的實例化類似函數(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()
實例擴展:
實例化過程:
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’) #實例化過程: #1. 是先產(chǎn)生一個stu1對象, #2. luffy_stu.__init__(’stu1’,’bao’,21,’male’)再將stu1對象傳入__init__構(gòu)造函數(shù)中實例化對象
以上就是python實例化對象的具體方法的詳細(xì)內(nèi)容,更多關(guān)于python如何實例化對象的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 如何在jsp界面中插入圖片2. jsp+servlet簡單實現(xiàn)上傳文件功能(保存目錄改進)3. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法4. 得到XML文檔大小的方法5. jsp實現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫的方法6. CSS3實例分享之多重背景的實現(xiàn)(Multiple backgrounds)7. ASP常用日期格式化函數(shù) FormatDate()8. JavaScrip簡單數(shù)據(jù)類型隱式轉(zhuǎn)換的實現(xiàn)9. XML入門的常見問題(二)10. ASP.NET Core實現(xiàn)中間件的幾種方式
