This article is participating in Python Theme Month. See the link to the event for more details

1 Introduction to object-oriented programming

There are two mainstream software development ideas today: one is process oriented and the other is object oriented. Process-oriented appeared early, with C language as the typical representative, and the development efficiency of small and medium-sized projects is very high, but it is difficult to apply to today’s mainstream large and medium-sized project development scenarios. Object-oriented emerged later, typically in languages such as Java or C++, and was more suitable for large-scale development scenarios. Both development ideas have their strengths and weaknesses. For the idea of process oriented: when a function needs to be implemented, the importance is to develop the steps and process, each step needs to be personally done, need to write their own code (do it by themselves) for the idea of object oriented: When I need to implement a function, I don’t care about the process and steps, but who will do it for me (lazy, find someone to do it for me). The three characteristics of object orientation are: encapsulation, inheritance, and polymorphism.

2 object-oriented nouns

Class: used to describe a collection of objects that have the same properties and methods. It defines properties and methods that are common to every object in the collection. An object is an instance of a class.

Class variables: Class variables are public throughout the instantiated object. Class variables are defined in the class and outside the function body. Class variables are generally not used as instance variables.

Data members: Class or instance variables that process data about a class and its instance objects.

Method override: If a method inherited from a parent class does not satisfy the needs of a child class, it can be overridden. This process is called method override, also known as method override.

Local variable: a variable defined in a method that only applies to the class of the current instance.

Instance variables: In the class declaration, attributes are represented as variables. Such variables, called instance variables, are declared inside the class declaration but outside the class’s other member methods.

Inheritance: A derived class inherits the fields and methods of a base class. Inheritance also allows an object of a derived class to be treated as a base object. For example, there is a design where an object of type Dog is derived from the Animal class, which simulates the “is an (IS-a)” relationship (example figure, where Dog is an Animal).

Instantiation: Creates an instance of a class, a concrete object of the class.

Method: a function defined in a class.

Object: An instance of a data structure defined by a class. An object consists of two data members (class variables and instance variables) and methods.

Three examples

#! /usr/bin/python # -* -coding: utf-8 -* -class Employee: ' 'def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print "Total Employee %d" % Employee.empCount def displayEmployee(self): print "Name : ", self.name, ", Salary: ", self.salaryCopy the code

The empCount variable is a class variable whose value will be shared between all instances of the class. You can use Employee. EmpCount access from an internal or external class.

The first method, the __init__() method, is a special kind of method called the class constructor or initialization method, which is called when an instance of the class is created

Self represents an instance of the class, and self is required to define a method of the class, although it does not have to be called with the corresponding arguments.

For more free Python learning materials, please go to the official account: Poesy Code. Now that I’m in. Let’s go with a “like”.