怎么在Python中實例化類-創(chuàng)新互聯(lián)

怎么在Python中實例化類?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

成都創(chuàng)新互聯(lián)長期為1000+客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為嵐皋企業(yè)提供專業(yè)的網(wǎng)站設計制作、成都做網(wǎng)站嵐皋網(wǎng)站改版等技術服務。擁有十年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

python中同樣使用關鍵字class創(chuàng)建一個類,類名稱第一個字母大寫,可以帶括號也可以不帶括號;

python中實例化類不需要使用關鍵字new(也沒有這個關鍵字),類的實例化類似函數(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中實例化類不需要使用關鍵字new(也沒有這個關鍵字),類的實例化類似函數(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()

運行結果:

Name: cuiyongyuan, age: 10
Name: yuanli, age: 10

看完上述內(nèi)容,你們掌握怎么在Python中實例化類的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

本文名稱:怎么在Python中實例化類-創(chuàng)新互聯(lián)
當前路徑:http://bm7419.com/article48/dcocep.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、外貿(mào)建站、網(wǎng)站收錄網(wǎng)站排名、網(wǎng)站導航云服務器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設