python中同樣使用關(guān)鍵字class創(chuàng)建一個(gè)類,類名稱第一個(gè)字母大寫,可以帶括號(hào)也可以不帶括號(hào);python中實(shí)例化類不需要使用關(guān)鍵字new(也沒有這個(gè)關(guān)鍵字),類的實(shí)例化類似函數(shù)調(diào)用方式;
#coding:utf-8
#創(chuàng)建一個(gè)類,類名稱第一個(gè)字母大寫,可以帶括號(hào)也可以不帶括號(hào)
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)建類的對象(實(shí)例化類)
python中實(shí)例化類不需要使用關(guān)鍵字new(也沒有這個(gè)關(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()
以上內(nèi)容為大家介紹了python培訓(xùn)之如何實(shí)例化對象,希望對大家有所幫助,如果想要了解更多Python相關(guān)知識(shí),請關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。