Object OO Oriented Programming
Base unit: Object
By encapsulating the data and functions in it, it is possible to achieve good reusability, flexibility and extensibility.
Two basic concepts for object-oriented: Classes and objects
Basic object-oriented elements: properties and methods
Properties and functions form a class.
Properties of the class:
Encapsulation,---> classes expose these capabilities, but hide specific implementation details, which is encapsulation.
Inheritance,---> Multiple inheritance
---> Several different classes that inherit from a class, and react differently when the same method is called. Will override the method
Defining classes
Class ClassName:
Statement1
....
Statementn
To set a property when creating a new class, you need the constructor of the class
constructor function:
def __init__ (self,[...):
Destructor: This is called when an object is reclaimed by the garbage collection mechanism. Different from the built-in function del
def __del__ (self,[...):
Now all the classes in Python3 are in the new class: The class that needs to continue object, some of the attributes of the object are inherited
Class NewStyle (object):
Pass
Two built-in functions: the Function!
Dir () returns the properties of an object
Type () returns the types of the object
Defining the properties of a class
Defined directly in the class:
Class Programer (object):
sex = ' Male '---> is shared by all objects, and this property is the same for all classes of objects.
Defined in the constructor:
Class Programer (object):
def __init__ (self,name,age):
Self.name = Name
Self.age = Age---> When the object is constructed, the property values of each different object are not the same
Access control:
Python does not provide the functionality of private attributes, no access control, and relies on programming specifications and naming conventions to constrain
Common attribute Definitions
Class Programer (object):
def __init__ (self,name,age,weight):
Self.name = Name---> No underline, public access available
Self._age = Age---> An underscore, this property generally represents the private property of a class, but it can also be accessed, but only the constraints of the programming specification, not the syntax
Self.__weight = Weight---> Two underscores, which implement partial private properties that are accessible in the class, are not accessible after the object is generated, and are only changed on the name of the property. A view of the properties obtained from the constructor can be viewed through print programer.__dict__---> representing only a disguised implementation of access to the private property, only changing the name.
Methods for defining classes
Functions and methods:
The function is called directly by calling the function name, and the method must be used in conjunction with the object. The function is just a piece of code for people to call, and the method belongs to a class and is called by another program. Whether attaching to a class is the most critical difference.
Everything in Python is object. You can think of a class's method as a property of a class.
Access control of the method is not
Definitions of commonly used methods:
Class Example (object):
def add (self):---> No underline is a common method
Pass
def _minus (self):---> has an underscore, which represents a private method, but is not constrained
Pass
def __multiply (self):---> has two underscores, just changed the name.
Pass
Two methods of adorners:
@classmethod
Called with the class name, not an object
@property
Call a method like an Access property. When you call a method normally: the name of the object. Method Name (), which is used without parentheses.
Example:
Class Programer (object):
Hobby = ' Play computer '
def __init__ (self,name,age,weight):
Self.name = Name
Self._age = Age
Self.__weight = Weight
@classmethod
def get_hobby (CLS):
Return Cls.hobby
@property
def get_weitht (self):
Return Self.__weight
def self_introduction (self):
print ' My name is%s \ni am%s years old\n '% (self.name,self,_age)
if __naem__ = = ' _main_ ':
Programer = Programer (' Albert ', 25,80)
Print dir (programer)
Print Programer.get_hobby ()! Note that it is called with the class name, uppercase!
Print Programer.get_weight! Note the same as the Access property, there is no parentheses behind!
Programer.self_introduction ()
Class of Inheritance! Object-oriented is very important, it makes a lot of code can be reused, is the advantage of object-oriented.
To define inheritance for a class:
Class Derivedclassname (Baseclassname):
<statement-1>
...
<statement-N>
The inherited subclass inherits the properties and methods of the parent class, and it can define itself, overriding the properties and methods of the parent class.
To invoke the parent class method When overriding the parent class, call the parent class's method with super ().
Class A (object):
Def method (Self,arg):
Pass
Class B (A):
Def method (Self,arg):
Super (B,self). Method (ARG)
There is also a way to call the parent class with the class name
Class A (object):
Def method (Self,arg):
Pass
Class B (A):
Def method (Self,arg):
A.method (ARG)
Type judgment for subclasses
Built-in functions:
Isinstance determines the type, whether it is a parent class, an object is subordinate to the parent class, and an object is considered to be the parent of the class in which the type is judged.
Issubclass Judging sub-class
Multiple inheritance, not many applications
Class Derivedclassname (BASE1,BASE2,BASE3):
<statement-1>
...
<statement-N>
The polymorphism of a class is doing the same thing, having different feedback and processes
Two main elements: inheritance and method rewriting. Because different subclasses inherit the same parent class, it is the method rewrite that makes different results when doing the same thing. Python can override the parent class method directly.
Python Magic Method
From the establishment of objects, property access, to the support of the operator, the support of the syntax, we can use the Magic method to define our class with great degrees of freedom.
There are two underscores before and after the method name
def __init__ (self):
The constructors and destructors in the front are all magic methods.
Object creation and initialization
The process of instantiating an object:
To create an object----------initialization object for a class
def __new__ (CLS): Def __init__ (self):
will return an object to init to initialize
Classes and operators
Comparison operators
__cmp__ (Self,other)
__eq__ (Self,other)
__lt__ (Self,other)
__gt__ (Self,other)
numeric operators
__add__ (Self,other)
__sub__ (Self,other)
__mul__ (Self,other)
__div__ (Self,other)
logical operators
__OR__ (Self,other)
__and__ (Self,other)
The manifestation of the class
The instantiation of a class can be typed out because it is converted to a string in print
__str__ converted to a human-looking string
__repr__ converted to machine-viewed strings
__unicode__
An object can be converted to a string by the appropriate method, which must be defined to inherit the corresponding magic method
Show Object Properties
The built-in method dir looks at the object properties, corresponding to the Magic method __dir__ to control the result of calling the Dir function, you can define the Dir magic method yourself.
Many of the built-in functions are supported by magic methods. can define the corresponding magic method
Property access control for a class
Set object properties: Note The case of infinite recursion! called itself when defined
__setattr__ (Self,name,value):
def __setattr__ (Self,name,value):
Self.__dict__[name] = value
Querying Object Properties
__getattr__ (self,name): query without access to call this method
def __getattribute__ (SELF,NAEM): Every access is called
Return Super (Programer,self). __getattribute (name) calls the GetAttribute method program of the parent class
Delete Object Properties
__delattr__ (Self,name):
Python Object-oriented summary!