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. ASP實(shí)現(xiàn)加法驗(yàn)證碼2. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁3. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)4. PHP循環(huán)與分支知識點(diǎn)梳理5. ASP基礎(chǔ)入門第三篇(ASP腳本基礎(chǔ))6. 利用CSS3新特性創(chuàng)建透明邊框三角7. 解析原生JS getComputedStyle8. css代碼優(yōu)化的12個技巧9. 前端從瀏覽器的渲染到性能優(yōu)化10. 讀大數(shù)據(jù)量的XML文件的讀取問題
