python如何實例化一個對象
python中同樣使用關(guān)鍵字class創(chuàng)建一個類,類名稱第一個字母大寫,可以帶括號也可以不帶括號;python中實例化類不需要使用關(guān)鍵字
new(也沒有這個關(guān)鍵字),類的實例化類似函數(shù)調(diào)用方式;
#coding:utf-8
#創(chuàng)建一個類,類名稱第一個字母大寫,可以帶括號也可以不帶括號
classStudent():
student_count=0
def__init__(self,name,salary):
self.name=name
self.age=salary
Student.student_count+=1
defdisplay_count(self):
print('Totalstudent{}'.format(Student.student_count))
defdisplay_student(self):
print('Name:{},age:{}'.format(self.name,self.age))
defget_class(self):
ifself.age>=7andself.age<8:
return1
ifself.age>=8andself.age<9:
return2
ifself.age>=9andself.age<10:
return3
ifself.age>=10andself.age<11:
return4
else:
return0
創(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()
以上內(nèi)容為大家介紹了python培訓之如何實例化一個對象,希望對大家有所幫助,如果想要了解更多Python相關(guān)知識,請關(guān)注IT培訓機構(gòu):千鋒教育。

猜你喜歡LIKE
相關(guān)推薦HOT
更多>>
python中的filter函數(shù)功能是什么?
python中的filter函數(shù)功能是什么?在python中,面對眾多的數(shù)據(jù),我們要過濾篩選出我們需要的數(shù)據(jù)。python中的filter函數(shù)就是起到了過濾篩選的作...詳情>>
2023-11-10 20:37:27
pythontime模塊是什么
pythontime模塊是什么在python中使用時間,就免不了和time模塊打交道,另外兩個模塊這個暫時先不做介紹。做time模塊的使用上,我們可以用它來對...詳情>>
2023-11-10 15:53:16
python是什么編程語言
python是什么編程語言1、說明是一種面向?qū)ο蟆⒔忉屝陀嬎銠C程序設(shè)計語言,由GuidovanRossum于1989年底發(fā)明,第一個公開發(fā)行版發(fā)行于1991年。Pyt...詳情>>
2023-11-10 15:21:05
python異常處理的兩種技巧
python異常處理的兩種技巧1、傳遞異常有時我們會在捕捉到一個異常后重新引發(fā)它(傳遞異常),實現(xiàn)起來很簡單,使用不帶參數(shù)的raise語句即可。deff...詳情>>
2023-11-10 14:49:39熱門推薦
python中的filter函數(shù)功能是什么?
沸python delattr函數(shù)如何使用?
熱python中pdb模塊怎么用?
熱Python如何截圖保存?
新python?中缺少module怎么辦?
python strftime和strptime的不同分析
python time.strptime的格式化
python中@contextmanager是什么?
python對象的三要素是什么
pythonGIL在Python多線程的應(yīng)用
python如何對多個CSV文件進行讀取
pythonif嵌套命令如何理解?
python對列表進行永久性或臨時排序的方法
python生成器調(diào)用方法引發(fā)異常
技術(shù)干貨







快速通道 更多>>
-
課程介紹
點擊獲取大綱 -
就業(yè)前景
查看就業(yè)薪資 -
學習費用
了解課程價格 -
優(yōu)惠活動
領(lǐng)取優(yōu)惠券 -
學習資源
領(lǐng)3000G教程 -
師資團隊
了解師資團隊 -
實戰(zhàn)項目
獲取項目源碼 -
開班地區(qū)
查看來校路線