视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
Python基础学习代码之面向对象编程
2020-11-09 09:13:43 责编:小采
文档


class AddrBookEntry(object):
 'address book entry class'
 def __init__(self,nm,ph):
 self.name = nm
 self.phone = ph
 print 'created instance for:',self.name
 def updatephone(self,newph):
 self.phone = newph
 print 'update phone for:',self.name
 def updatename(self,newname):
 self.name = newname
 print 'update phone for:',self.phone
john = AddrBookEntry('xiewenbin','13711710490')
print john.name
print john.phone
john.updatephone('18617311540')
john.updatename('xwb')
print john.phone
print john.name
class EmpAddrBookEntry(AddrBookEntry):
 'employee address book entry class'
 def __init__(self,nm,ph,id,em):
 AddrBookEntry.__init__(self,nm,ph)
 self.empid = id
 self.email = em
 def updateemail(self,newem):
 self.email = newem
 print 'update email address for:',self.name
jone = EmpAddrBookEntry('jone doe','408-555-1212',42,'543361609@qq.com')
print jone.name
print jone.phone
print jone.email
jone.updatephone('18617311541')
print jone.phone
jone.updateemail('186@qq.com')
print jone.email
class HotelRoomCalc(object):
 'hotel room rate calculator'
 def __init__(self,rt,sales=0.084,rm=0.1):
 '''hotelroot calc default arguments:
 sales tax == 8.5% and room tax == 10%'''
 self.salestax = sales
 self.roomtax = rm
 self.rootrate = rt
 def cacltotal(self,days=1):
 'calculator total;default to daily rate'
 daily = round((self.rootrate * (1 + self.roomtax + self.salestax)),2)
 return float(days) * daily
sfo = HotelRoomCalc(299)
print sfo.cacltotal(3)
class TestStaticMethod(object):
 @staticmethod
 def foo():
 print 'calling static method foo()'
class TestClassMethod(object):
 @classmethod
 def foo(cls):
 print 'calling class method foo()'
 print 'foo() is part of class:',cls.__name__
class C(object):
 foo = 100
print C.foo + 1
class Myclass(object):
 'myclass class definition'
 myversion = 19.0
 def showmyversion(self):
 print Myclass.myversion
mc = Myclass()
mc.showmyversion()
print dir(Myclass)
print Myclass.__dict__
"""
class InstCt(object):
 count = 0
 def __init__(self):
 InstCt.count += 1
 def __del__(self):
 InstCt.count -= 1
 def howmany(self):
 return InstCt.count
a = InstCt()
b = InstCt()"""
x = 3 + 0.14j
print x.__class__
print [type(getattr(x,i)) for i in ('conjugate','imag','real')]
class Foo(object):
 x = {2003:'poe2'}
foo = Foo()
print foo.x
foo.x[2004] = 'xie'
print foo.x
print Foo.x
del foo.x
print foo.x
print Foo.x

下载本文
显示全文
专题