詳解Python中類的定義與使用
類顧名思義,就是一類事物、或者叫做實(shí)例,它用來描述具有共同特征的一類事物。我們在python中聲明類的關(guān)鍵詞是class,類還有功能和屬性,屬性就是這類事物的特征,而功能就是它能做什么,也是就是方法或者函數(shù)。我們?nèi)匀挥美觼碚f明問題。
目標(biāo):
1.類的定義
2.父類,子類定義,以及子類調(diào)用父類
3.類的組合使用
4.內(nèi)置功能
1.類的定義
代碼如下:
#!/usr/bin/env python
#coding:utf8
class Hotel(object):
"""docstring for Hotel"""
def __init__(self, room, cf=1.0, br=15):
self.room = room
self.cf = cf
self.br = br
def cacl_all(self, days=1):
return (self.room * self.cf + self.br) * days
if __name__ == '__main__':
stdroom = Hotel(200)
big_room = Hotel(230, 0.9)
print stdroom.cacl_all()
print stdroom.cacl_all(2)
print big_room.cacl_all()
print big_room.cacl_all(3)
2.父類、子類以及調(diào)用父類
代碼如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 父類
class AddBook(object):
def __init__(self, name, phone):
self.name = name
self.phone = phone
def get_phone(self):
return self.phone
# 子類,繼承
class EmplEmail(AddBook):
def __init__(self, nm, ph, email):
# AddBook.__init__(self, nm, ph) # 調(diào)用父類方法一
super(EmplEmail, self).__init__(nm, ph) # 調(diào)用父類方法二
self.email = email
def get_email(self):
return self.email
# 調(diào)用
if __name__ == "__main__":
Detian = AddBook('handetian', '18210413001')
Meng = AddBook('shaomeng', '18210413002')
print Detian.get_phone()
print AddBook.get_phone(Meng)
alice = EmplEmail('alice', '18210418888', 'alice@xkops.com')
print alice.get_email(), alice.get_phone()
3.類的組合使用
代碼如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
1.class類的組合使用
2.手機(jī)、郵箱、QQ等是可以變化的(定義在一起),姓名不可變(單獨(dú)定義)。
3.在另一個(gè)類中引用
'''
class Info(object):
def __init__(self, phone, email, qq):
self.phone = phone
self.email = email
self.qq = qq
def get_phone(self):
return self.phone
def update_phone(self, newphone):
self.phone = newphone
print "手機(jī)號(hào)更改已更改"
def get_email(self):
return self.email
class AddrBook(object):
'''docstring for AddBook'''
def __init__(self, name, phone, email, qq):
self.name = name
self.info = Info(phone, email, qq)
if __name__ == "__main__":
Detian = AddrBook('handetian', '18210413001', 'detian@xkops.com', '123456')
print Detian.info.get_phone()
Detian.info.update_phone(18210413002)
print Detian.info.get_phone()
print Detian.info.get_email()
4.內(nèi)置功能(函數(shù)()加與不加的區(qū)別)
代碼如下:
#!/usr/bin/env python
#coding:utf8
class Books(object):
def __init__(self, title, author):
self.title = title
self.author = author
def __str__(self):
return self.title
def __repr__(self):
return self.title
def __call__(self):
print "%s is written by %s" %(self.title, self.author)
if __name__ == '__main__':
pybook = Books('Core Python', 'Wesley')
print pybook
pybook()
#!/usr/bin/env python
#coding:utf8
class Number(object):
"""Custum object
add/radd -> +;
sub/rsub -> -;
mul/rmul -> *;
div/rdiv -> /;
"""
def __init__(self, number):
self.number = number
def __add__(self, other):
return self.number + other
def __radd__(self, other):
return self.number + other
def __sub__(self, other):
return self.number - other
def __rsub__(self, other):
return other - self.number
def __gt__(self, other):
if self.number > other:
return True
return False
if __name__ == '__main__':
num = Number(10)
print num + 20
print 30 + num
print num - 5
print 11 - num
print num > 20
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python人工智能之路 jieba gensim 最好別分家之最簡單的相似度實(shí)現(xiàn)
這篇文章主要介紹了Python人工智能之路 jieba gensim 最好別分家之最簡單的相似度實(shí)現(xiàn) ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
mac使用python識(shí)別圖形驗(yàn)證碼功能
這篇文章主要介紹了mac使用python識(shí)別圖形驗(yàn)證碼功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
python實(shí)現(xiàn)linux下使用xcopy的方法
這篇文章主要介紹了python實(shí)現(xiàn)linux下使用xcopy的方法,可實(shí)現(xiàn)模仿windows下的xcopy命令功能,需要的朋友可以參考下2015-06-06
Python實(shí)現(xiàn)更改圖片尺寸大小的方法(基于Pillow包)
這篇文章主要介紹了Python實(shí)現(xiàn)更改圖片尺寸大小的方法,結(jié)合實(shí)例形式分析了Python基于Pillow包更改圖片屬性的相關(guān)技巧,需要的朋友可以參考下2016-09-09
Python基礎(chǔ)數(shù)據(jù)類型tuple元組的概念與用法
元組(tuple)是 Python 中另一個(gè)重要的序列結(jié)構(gòu),和列表類似,元組也是由一系列按特定順序排序的元素組成,這篇文章主要給大家介紹了關(guān)于Python基礎(chǔ)數(shù)據(jù)類型tuple元組的概念與使用方法,需要的朋友可以參考下2021-07-07
Pandas+openpyxl進(jìn)行Excel處理詳解
這篇文章主要為大家詳細(xì)介紹了如何使用pandas和openpyxl庫對多個(gè)Excel文件進(jìn)行多種處理的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2025-02-02
python函數(shù)的默認(rèn)參數(shù)請勿定義可變類型詳解
這篇文章主要介紹了python函數(shù)的默認(rèn)參數(shù)請勿定義可變類型詳解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Python繪制分段函數(shù)的實(shí)現(xiàn)示例
本文主要介紹了Python繪制分段函數(shù)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

