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.phonejohn = addrbookentry (' Xiewenbin ', ' 13711710490 ') print john.nameprint John.phonejohn.updatephone (' 18617311540 ') jOhn.updatename (' XWB ') print john.phoneprint john.nameclass 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.namejone = empaddrbookentry (' Jone doe ', ' 408-555-1212 ', A, ' [email protected] ') print jone.nameprint jone.phoneprint Jone.emailjone.updatephone (' 18617311541 ') print jone.phonejone.updateemail (' [email protected] ') print jone.emailclass 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) * dailysfo = 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 = 100print c.foo + 1class myclass ( Object): ' Myclass class definition ' myversion = 19.0 def showmyversion (self): print myclass.myversIonmc = 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.counta =  INSTCT () B = INSTCT () "" "X = 3 + 0.14jprint x.__class__print [type (GetAttr (X,i)) for i in (' conjugate ', ' imag ', ' real ')]class foo (object): x = {2003: ' Poe2 '}foo = foo () print foo.xfoo.x[2004] = ' Xie ' Print foo.xprint foo.xdel foo.xprint foo.xprint foo.x
This article is from the "XWB" blog, make sure to keep this source http://xiewb.blog.51cto.com/11091636/1792281
Object-oriented programming of Python's Basic Learning Code