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. CentOS郵箱服務(wù)器搭建系列——SMTP服務(wù)器的構(gòu)建( Postfix )2. 詳解PHP laravel中的加密與解密函數(shù)3. PHP與MYSQL數(shù)據(jù)庫連接4. asp文件用什么軟件編輯5. docker /var/lib/docker/aufs/mnt 目錄清理方法6. PHP輸入流php://input的使用分析7. IntelliJ IDEA創(chuàng)建web項(xiàng)目的方法8. PHP基礎(chǔ)之生成器4——比較生成器和迭代器對象9. Docker 啟動Redis 并設(shè)置密碼的操作10. ASP新手必備的基礎(chǔ)知識

網(wǎng)公網(wǎng)安備