python實例化對象的具體方法
python中同樣使用關鍵字class創(chuàng)建一個類,類名稱第一個字母大寫,可以帶括號也可以不帶括號;python中實例化類不需要使用關鍵字new(也沒有這個關鍵字),類的實例化類似函數(shù)調用方式;
# 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ù)調用方式。
student1 = Student('cuiyongyuan',10)
student2 = Student('yuanli', 10)
student1.display_student()
student2.display_student()
student1_class = student1.get_class()
student2_class = student2.get_class()
實例擴展:
實例化過程:
class luffy_stu:
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
def eat(self):
pass
if __name__=="__main__":
stu1 = luffy_stu('bao',21,'male')
#實例化過程:
#1. 是先產(chǎn)生一個stu1對象,
#2. luffy_stu.__init__('stu1','bao',21,'male')再將stu1對象傳入__init__構造函數(shù)中實例化對象
以上就是python實例化對象的具體方法的詳細內容,更多關于python如何實例化對象的資料請關注腳本之家其它相關文章!
相關文章
python使用wxpython開發(fā)簡單記事本的方法
這篇文章主要介紹了python使用wxpython開發(fā)簡單記事本的方法,涉及Python使用wxPython實現(xiàn)桌面圖形應用程序的技巧,需要的朋友可以參考下2015-05-05
pycharm遠程連接vagrant虛擬機中mariadb數(shù)據(jù)庫
這篇文章主要介紹了pycharm遠程連接vagrant虛擬機中mariadb數(shù)據(jù)庫,需要的朋友可以參考下2020-06-06
Python+opencv 實現(xiàn)圖片文字的分割的方法示例
這篇文章主要介紹了Python+opencv 實現(xiàn)圖片文字的分割的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07
Django+uni-app實現(xiàn)數(shù)據(jù)通信中的請求跨域的示例代碼
這篇文章主要介紹了Django+uni-app實現(xiàn)數(shù)據(jù)通信中的請求跨域的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10
Python pandas dataframe之重命名相同列名
這篇文章主要介紹了Python pandas dataframe之重命名相同列名方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
pyecharts中from pyecharts import options
本文主要介紹了pyecharts中from pyecharts import options as opts報錯問題以及解決辦法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07
Python數(shù)據(jù)分析:手把手教你用Pandas生成可視化圖表的教程
今天小編就為大家分享一篇Python數(shù)據(jù)分析:手把手教你用Pandas生成可視化圖表的教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12

