創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!

小編給大家分享一下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 = name
self.age = salary
Student.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 1
if self.age >= 8 and self.age < 9:
return 2
if self.age >= 9 and self.age < 10:
return 3
if self.age >= 10 and self.age < 11:
return 4
else:
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()看完了這篇文章,相信你對python實例化對象的方法有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝各位的閱讀!
當(dāng)前名稱:python實例化對象的方法-創(chuàng)新互聯(lián)
URL分享:http://www.chinadenli.net/article28/pdocp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、動態(tài)網(wǎng)站、ChatGPT、企業(yè)網(wǎng)站制作、虛擬主機、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容