Describes the definition and use of classes in Python and the definition of python.
A class, as its name implies, is a type of thing or an instance used to describe a type of thing with common characteristics. In python, we declare class keywords as class. Classes also have functions and attributes. attributes are the features of such things, and functions are what they can do, namely, methods or functions. We still use examples to illustrate the problem.
Objectives:
1. Class Definition
2. parent class, subclass definition, and subclass call parent class
3. Combined use of Classes
4. built-in functions
1. Class Definition
The Code is as follows:
#!/usr/bin/env python#coding:utf8class 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) * daysif __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. parent class, subclass, 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, email): # AddBook. _ init _ (self, nm, ph) # Call the parent class method super (EmplEmail, self ). _ init _ (nm, ph) # Call parent class method 2 self. email = email def get_email (self): return self. email # Call if _ name _ = "_ main _": Detian = AddBook ('handetianc', '000000') Meng = AddBook ('shaomeng ', '20140901') print Detian. get_phone () print AddBook. get_phone (Meng) alice = EmplEmail ('Alice ', '000000', 'Alice @ xkops.com') 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 combination 2. mobile phones, mailboxes, and QQ can be changed (defined together), and the name cannot be changed (defined separately ). 3. reference '''class Info (object) 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 "cell phone number 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 ('handetia ', '000000', 'destan @ xkops.com ', '000000') print Detian.info. get_phone () Detian.info. update_phone (18210413002) print Detian.info. get_phone () print Detian.info. get_email ()
4. built-in functions (difference between function () Addition and not addition)
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 is 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(10) print num + 20 print 30 + num print num - 5 print 11 - num print num > 20
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.