Goal:
1. Definition of class
2. Parent class, subclass definition, and subclass call parent class
3. Combined use of classes
4. Built-in features
1. Definition of class
The code is as follows:
#!/usr/bin/env Python#coding:utf8class Hotel (object): "" "DocString for Hotel" "" def __init__ (self, guest, cf= 1.0, Br=15): self.room = guest self.cf = CF self.br = BR def cacl_all (self, Days=1): return ( Self.room * SELF.CF + self.br) * Daysif __name__ = "__main__": stdroom = Hotel (big_room) = Hotel (0.9)
print stdroom.cacl_all () print Stdroom.cacl_all (2) print big_room.cacl_all () print Big_room.cacl _all (3)
2. Parent class, child class, and calling parent class
The code is as follows:
#!/usr/bin/env python#-*-coding:utf-8-*-# Parent class Addbook (object): def __init__ (self, Name, phone): Self.name = name Self.phone = Phone def get_phone (self): return self.phone# subclass, inheriting class Emplemail (Addbook): def __init__ (self, NM, ph, e-mail): # addbook.__init__ (self, NM, ph) # Call the parent class method a super (Emplemail, self). __init __ (nm, ph) # Call the parent class method two Self.email = email def get_email (self): return self.email# call 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. Combined use of classes
The code is as follows:
#!/usr/bin/env python#-*-coding:utf-8-*-"1.class class combination use 2. Mobile phones, e-mail, QQ, etc. can be changed (defined together), the name is immutable (defined separately). 3. Refer to "Class Info" in another class: 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 "Phone number change changed" def get_email (self): return Self.emailclass 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. Built-in function (function () plus and no difference)
The code is as follows:
#!/usr/bin/env python#coding:utf8class 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 was written by%s"% (Self.title, self.author) if __name__ = = ' __main__ ': pybook = Books (' Core Python ', ' Wesley ') print Pybook pybook ()
--------------------------------------------------------------------------------------------------------------- -------------------------------------------------
#!/usr/bin/env python#coding:utf8class 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 falseif __name__ = = ' __main__ ': num = number print num + print + num print num-5 print 11-num print num > 20
Definition and use of classes in Python