Python中類的定義與使用

2022-07-17 07:48:10 字數 4409 閱讀 5085

目標:

1.類的定義

2.父類,子類定義,以及子類呼叫父類

3.類的組合使用

4.內建功能

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.父類、子類以及呼叫父類

**如下:

#

!/usr/bin/env python

#-*- coding: utf-8 -*-#父類

class

addbook(object):

def__init__

(self, name, phone):

self.name =name

self.phone =phone

defget_phone(self):

return

self.phone

#子類,繼承

class

emplemail(addbook):

def__init__

(self, nm, ph, email):

#addbook.__init__(self, nm, ph) # 呼叫父類方法一

super(emplemail, self).__init__

(nm, ph) # 呼叫父類方法二

self.email =email

defget_email(self):

return

self.email#呼叫

if__name__ == "

__main__":

detian = addbook('

handetian

', '

18210413001')

meng = addbook('

shaomeng

', '

18210413002')

print

detian.get_phone()

print

addbook.get_phone(meng)

alice = emplemail('

alice

', '

18210418888

', '

[email protected]')

print alice.get_email(), alice.get_phone()

3.類的組合使用

**如下:

#

!/usr/bin/env python

#-*- coding: utf-8 -*-

'''1.class類的組合使用

2.手機、郵箱、qq等是可以變化的(定義在一起),姓名不可變(單獨定義)。

3.在另乙個類中引用

'''class

info(object):

def__init__

(self, phone, email, qq):

self.phone =phone

self.email =email

self.qq =qq

defget_phone(self):

return

self.phone

defupdate_phone(self, newphone):

self.phone =newphone

print

"手機號更改已更改

"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

', '

[email protected]

', '

123456')

print

detian.info.get_phone()

detian.info.update_phone(18210413002)

print

detian.info.get_phone()

print detian.info.get_email()

4.內建功能(函式()加與不加的區別)

**如下:

#

!/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

Python類的定義與使用

class person def init self,name,age,ce self.name name self.self.age age self.ce ce def grassland self 注釋 草叢戰鬥,消耗200戰鬥力 self.ce self.ce 300 def practic...

python 類的定義與使用

1.類定義語法 class car 類名的首字母一般要大寫 def infor self print this is a car car car 例項化物件 car.infor 物件名.成員,訪問成員方法 print isinstance car,car 內建方法isinstance 來測試乙個物件...

Python中類的定義及使用

類是一些有共同特徵和行為事務事物的抽象概念的總和。從中可以看出,方法只能使用例項直接呼叫 無需傳self引數 而使用類呼叫必須傳入例項物件 屬性可以使用例項呼叫,也可以使用類直接呼叫,因此可以看出,其實self就是例項本身,在例項呼叫方法時傳入。被例項化的物件會被編譯器默默的傳入方法的括號中,作為第...