One: Get to know a couple of object-oriented concepts first
class: used to describe a collection of objects that have the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.
Class variables: class variables are common throughout the instantiated object. Class variables are defined in the class and outside the body of the function. Class variables are not typically used as instance variables.
data members: class variables or instance variables are used to manipulate the data related to the class and its instance objects.
method Overrides: If a method inherited from a parent class does not meet the requirements of a subclass, it can be overridden, a process called method overrides (override), also known as a method override
instance variable: A variable defined in a method that acts only on the class of the current instance.
inheritance: A derived class (derived class) that inherits the fields and methods of the base class. Inheritance also allows the object of a derived class to be treated as a base class object
instantiation: Creating an instance of a class, the concrete object of the class
method: a function defined in a class.
object: An instance of a data structure defined by a class. The object consists of two data members (class variables and instance variables) and methods.
Create Class
Use the class statement to create a new class, followed by the name of the class and ending with a colon.
-Self represents an instance of a class, and self is necessary when defining a method of a class, although it is not necessary to pass in the corresponding parameter when calling
Self represents an instance of a class, not a class
There is only one special difference between a method of a class and a normal function--they must have an extra first parameter name , according to the Convention its name is self.
Creating an Instance Object
The instantiation class is typically used in other programming languages with the keyword new, but in Python there is no such keyword, and the instantiation of the class is similar to how the function is called.
The following uses the name of the class to instantiate and accept the arguments through the __init__ method.
Tianfeng = People ("Tian Feng", 18) # Objects instantiated by class
Accessing properties
You can use the dot (.) To access the properties of the object. Access the class variable using the name of the following class:
Tianfeng.echo_self ()
Python built-in class properties
__DICT__: Properties of the Class (contains a dictionary, consisting of the data properties of the Class)
__DOC__: Document string for Class
__NAME__: Class name
__MODULE__: The module where the class definition resides (the full name of the class is ' __main__.classname ', if the class is in an import module mymod, then classname.__module__ equals Mymod)
Python Object Destruction (garbage collection)
Python uses the simple technique of reference counting to track and recycle garbage.
Inside Python is a record of how many references each object in use has.
An internal tracking variable, called a reference counter.
When the object is created, a reference count is created, and when the object is no longer needed, that is, the reference count of the object becomes 0 o'clock, and it is garbage collected. However, recycling is not "immediate", and the interpreter uses the garbage object to reclaim the memory space at the appropriate time.
The garbage collection mechanism not only targets objects with a reference count of 0, but can also handle circular references. Circular references refer to two of objects referencing each other, but no other variables refer to them. In this case, using only the reference count is not enough. The Python garbage collector is actually a reference counter and a cyclic garbage collector. As a supplement to the reference count, the garbage collector also pays attention to objects that are allocated a large amount (and those that are not destroyed by reference counting). In this case, the interpreter pauses to attempt to clean up all unreferenced loops.
Inheritance of Classes
One of the main benefits of object-oriented programming is the reuse of code, and one way to implement this reuse is through inheritance mechanisms. Inheritance can be fully understood as a type and subtype relationship between classes.
What to note: Inherit the syntax class derived class name ( base class name )://... The base class name is written in parentheses, and the base class is specified in the tuple at the time the class is defined.
Some of the characteristics of inheritance in Python:
1: The construction of the base class in inheritance (The __init__ () method) is not automatically called, it needs to be called specifically in the construction of its derived class.
2: When calling a method of the base class, you need to prefix the class name of the base class with the Self argument variable. Unlike calling a normal function in a class, you do not need to take the self argument
3:python always looks for a method of the corresponding type first, and if it cannot find the corresponding method in the derived class, it begins to look in the base class one by one. (Find the method that was called in this class before you can find it in the base class).
Class properties and private properties of method classes
__private_attrs: Two underscores begin with, declaring that the property is private and cannot be used outside of the class or accessed directly. self.__private_attrswhen used in methods inside a class.
Methods of the class
Inside a class, you can define a method for a class using the def keyword, unlike a generic function definition, which must contain the parameter self and be the first argument
Private methods of the class
__private_method: Two underscores, declares that the method is a private method and cannot be called outside of a class. Calling Self.__private_methods inside a class
Single underline, double underline, and tail-to-bottom double underline description:
__foo__: Defines a special method, similar to __init__ () .
_foo: A variable of type protected that begins with a single underscore, that is, the protection type can only allow access to itself and subclasses, not to the From module import *
__foo: A double underline is a variable of a private type, which is allowed to be accessed only by the class itself.
First attribute of class: encapsulation
#-What is self essentially? instantiated objects
#-Encapsulation is the encapsulation of an instantiated object and its attributes to facilitate the use of other functions;
#-Two ways to access object properties:
# Self.name
# Tianfeng.name
# When your class is a new class, multiple inheritance algorithms are breadth-first;
# When your class is a classic class, multiple-inheritance algorithms are depth-first;
# Class of properties, in memory only need to be stored once;
# in the constructor of the property, each instantiation of an object, it needs to be stored once;
This article is from the "12462896" blog, please be sure to keep this source http://12472896.blog.51cto.com/12462896/1964030
Python Object-oriented