Python Object-Oriented Programming summary (Part 1)
Before I learned python, I always thought that python is a scripting language and cannot be programmed using the surface object method. After I learned python, I found that I was wrong, python not only supports face-to-face objects, but also supports many users. I have been learning the Java language since I started to use programming, so the idea of Object-based programming is deeply rooted in my mind, let me look at the transformation from object-oriented programming to process-oriented programming, so I will summarize the methods and processes of python object-oriented programming. I will analyze the elements of object-oriented architectureClass and instance, attributes, methods, encapsulation, inheritance, and PolymorphismIf you have any questions, please actively point out that my version is python2.7!
I. Classes and Instances
Object-oriented is a programming idea that maps things in real life into the program world. Therefore, we can use the ideas in real life to consider programming problems, this makes programming easier. After all, we have been living in real life for decades.
Let's take a look at the basic form of classes in python.
Class Movie (object): def _ init _ (self, name, length): self. name = name self. length = length def print_name (self): print 'movie name: % s' % (self. name ,)
Class Movie (object): it is a fixed writing method. class indicates that a class is being declared, Movie indicates the class name, and (object) indicates that it inherits from those parent classes, Just Like java, all classes are inherited from the object class, so if you write a class that does not inherit from other classes, write it as inherited from the object. When it inherits from the object, the brackets together with the objects in the brackets can be omitted as: class Movie:
First Method__init__Is the constructor. The first parameter of the constructor is always self, which indicates the object of this class. When constructing an object, the self parameter does not need to be written. The python compiler will add it by itself, the role of the constructor is to assign values to the self object, and assign the name and length of the movie to the self object.
For the above Movie class, we can use the following code to call it:
Import OOPmovie = OOP. Movie ('Big holy return ', 90) movie. print_name ()
The output result is: the name of the movie is "Return of the great saint ".
If no parameters need to be passed to the object, the constructor can also be omitted. For example:
Class Movie (object): def print_info (self): print 'this is a Movie class'
In this way, no parameters need to be passed during object construction, for example:
import OOPmovie = OOP.Movie()movie.print_info()
The output result is: this is a movie class.
Ii. Attributes
1. instance attributes
Instance attributes are owned by objects, and each object has its own set of attributes. The instance attributes of different objects do not interfere with each other. In python__init__The attribute declared in the method is the instance attribute. Of course, you can also use self in any method in the class. var = something declares instance attributes, but I personally think this will make the program very messy and prone to errors. Here is an example:
OPP
#-*-Coding: UTF-8-*-class Movie (object): def _ init _ (self, name, length): self. name = name self. length = length def print_name (self): self. aaa = '200' print 'the name of the movie is % s' % (self. name ,)
OOPTest
#-*-Coding: UTF-8-*-import OOPmovie1 = OOP. movie ('Return of the grandcan', 90) movie2 = OOP. movie ('mermaid ', 120) print movie1.nameprint movie2.namemovie1. print_name () print movie1.aaa # only after the print_name method is called Will aa be defined as movie1.name = 'start' print movie1.nameprint movie2.name # movie1 does not affect movie2
Output result:
Return of the secret
Mermaid
The name of the movie is: Return of the secret
123
Wind Power
Mermaid
2. class attributes
Class attributes belong to this class. All instances share a variable. If an instance changes the class attribute value, all instances will be affected. The declaration of class attributes is independent of all methods. The declaration method is as follows:
OOP
#-*-Coding: UTF-8-*-class Movie (object): count = 1 # class attribute def _ init _ (self, name, length): self. name = name self. length = length def print_name (self): self. aaa = '200' print 'the name of the movie is % s' % (self. name ,)
The preceding count is a class attribute, which is similar to the static variable in java. You can use the class name. you can also use the instance. the class attribute is generally used to count objects or share variables between different instances. Let's take a look at the example below to understand the class variables.
#-*-Coding: UTF-8-*-import OOPmovie1 = OOP. movie ('Return of the grandcan', 90) movie2 = OOP. movie ('mermaid ', 120) print OOP. movie. count #1 by class name. use count to call print movie2.count #1 through the instance. use the count method to call movie1.count = 3 print OOP. movie. count #1 print movie1.count #3 print movie2.count # 1OOP. movie. count = 3 print OOP. movie. count #3 print movie1.count #3 print movie2.count #3
The comment is the output result. We can see that if the class name is used. the value of all instances will change,. the value of the corresponding instance is changed.
Iii. Methods
Python methods do not strictly distinguish static methods from instance methods as C # and Java compiling languages. That is to say, the Python static method, the class method and the instance method are only different in the call, type and instance can call.
1. instance method
The instance method can be called by a class instance through the instance. the print_name method in the preceding example is the instance method. When the instance method is used to declare the input parameter method, one parameter is missing, because the first parameter self is not provided. In addition, the instance method can also be called by class in the form of class name. Method Name (self). You must manually add the self variable, that is, the instance of the class.
2. Class Methods
Class methods can be called by classes and class instances, but class methods cannot access instance variables. They can only be used to declare class variables.@ ClassmethodAnd the first parameter in the method is cls rather than self. Like the instance method, the first parameter does not need to be passed during use. The python compiler will automatically add the parameter. The class method is more like the static method in java.
3. Static Method
Static methods can be called by classes and class instances, and cannot access instance variables and class variables. Only parameters passed in by methods can be added before methods.@ StaticmethodThe static method does not have default parameters. The static method is a bit like the Function Tool library.
OOP
#-*-Coding: UTF-8-*-class Movie (object): count = 1 def _ init _ (self, name, length): self. name = name self. length = length # instance method def print_name (self): self. aaa = '200' print 'the name of the movie is % s' % (self. name,) # class method @ classmethod def print_len (cls): print 'movie length: % d' % (cls. count,) # static method @ staticmethod def print_info (str): print ': % s' % (str ,)
OOPTest
#-*-Coding: UTF-8-*-import OOPmovie1 = OOP. movie ('Return of the holy king', 90) movie1.print _ name () # Call the instance method OOP through an instance. movie. print_name (movie1) # If you call an instance method through a class, you must manually input the instance OOP. movie. print_len () # Call the class method movie1.print _ len () # Call the class method OOP through an instance. movie. print_info ('haha ') # Call the static method movie1.print _ info ('haha') through the class # Call the static method through the instance
There are still a lot of things left. I can't finish writing them all at once. I will discuss encapsulation, inheritance, and polymorphism in the next article. Thank you for your support!