Object-Oriented Programming
It is a kind of program programming model with object concept, and it is also an abstract policy of program development. It may contain data, properties, code, and methods. Object refers to an instance of a class. It takes the object as the basic unit of the program, encapsulates the program and data to improve the reusability, flexibility and extensibility of the software, and the program in the object can access and modify the data associated with the object frequently. In object-oriented programming, computer programs are designed to be objects that are related to each other.
Object-oriented programming can be thought of as a kind of idea which contains various independent and mutually invoking objects in the program, which is opposite to the traditional idea: the traditional program design advocates treat the procedure as a set of functions, or a series of instructions to the computer directly. Each object in object-oriented programming should be able to accept data, process data, and communicate it to other objects, so they can all be thought of as a small "machine", or object. It has been proved that object-oriented program design promotes the flexibility and maintainability of the program, and is widely used in large-scale project designs. In addition, supporters claim that object-oriented programming is easier to learn than ever before, because it allows people to design and maintain programs more easily, making them easier to analyze, design, and understand. Opponents deny this in some areas.
When we refer to object-oriented, it refers not only to a programming method. It is more of a program development approach. In this regard, we have to learn more about object-oriented system analysis and object-oriented design (object oriented, short Ood). Many popular programming languages are object-oriented, and their style is to create instances through objects.
Class
Class: A collection of objects that describe the same properties and methods that define the properties and methods common to each object in the collection.
definition of Class
For the definition of a class, different languages have their own different definitions, in Python the definition of the class is relatively simple, as follows:
Format:
class class_name (object): def func (self) : pass               &NBSP, .....
In either Java or Python, where each class has its own parent, the class defined by default is a subclass of object, as is generally not indicated.
For the definition of a method of a class, each method needs to define a self parameter that, when invoking a class method, does not need to pass a value to self, which is equivalent to the this pointer in Java or C + +, which distinguishes the class method from the different methods.
the access rights of the class
There are private and common differences between the properties and methods in a class, so what is private and what is common?
Private Properties/methods: Refers to properties/methods that cannot be called by external methods
Common Properties/methods: Refers to properties/methods that can be accessed through an external method
Definition of private property:
Format:
Class Class_name (object): __prive=2312
In fact, the definition of a private variable is simple, that is, the variable is preceded by two underscore (__).
Definition of Private method:
Format:
Class Class_name (object): Def __prive_func (self): ....
The definition of a private method is the same as the definition of a private property, preceded by two underscores (__)
the proprietary method of the class
(1). Constructors
What is a constructor method:
The construction method is a special method that does not return the value, the object is created by the construction method, its function is to complete the initialization of the object. Constructor methods are called automatically when a class instantiates an object.
Format:
Class Class_name (object): Def __init__ (self): ....
When a class does not write a constructor, the system default construction method is called automatically when the object is created.
(2). destructor
What is a destructor? &NBSP the
destructor (destructor), in contrast to the constructor, automatically executes the destructor when the object ends its life cycle (for example, the function where the object is already called). Destructors are often used to "clean up the aftermath" of work.
Format:
class class_name (object): def __init__ (self): .... def __del__ (self) : ..
(3). Get__score (self) function
This function is a method that Python is dedicated to access the private properties of a class, and for classes we encapsulate, but sometimes we have to access some private properties in the class, which is defined in the class that this method can return a private variable.
Format:
class class_name (object): def __init__ (self,name): self.__name=name def Get__score (self): return self__name def __del__ (self): &NBSP, .....
(4). Set__attrs (Self,bian) function
For private variables We cannot do this in a generic way, so changing the value of a private variable is a fantasy. Python provides us with the Set__attrs () method, which is used to change the value of a private variable externally.
Format:
Class Class_name (object): Def __init__ (self,name): Self.__name=name def get__score (self): Retu RN Self__name def set__attrs (self,name): Self.__name=name def __del__ (self): ....
instantiation of a class
Before we introduce some of the methods and definitions of a class, how can you create an object of a class to instantiate? In fact, this is very simple, as follows:
Format:
variable = class name (parameter)
Class class_name (object): def __init__ (self,name): self.__name=name def get__score (self): return self__name def set__attrs (Self,name): self.__name=name def __del__ (self): ...bian=calss_name (' Python ') print (Bian.get__score ()) Bian.set_attrs (' Java ') print (Bian.get__score ()) Run Result: Pythonjava
-
for each object-oriented programming, there are three major features encapsulated, inherited, polymorphic. Let's introduce them.
-
encapsulation
Encapsulation is the principle of global use to hide excess information in other areas of the package, unlike polymorphism, which allows the user to make method calls to objects that do not know the class, and the encapsulation can be used without concern about how the object is constructed, directly using the
-
inheritance refers to the child class inheriting the properties and methods of the parent class and achieving the common purpose of the code.
Features:
(1): In inheritance, the construction method of the base class is not automatically called and needs to be called specifically in the constructor method of the subclass.
(2): You need to prefix the class name of the base class with the self parameter variable when calling the release of the base class. Unlike calling a normal function in a class, you do not need the self parameter.
(3): In Python, the method of the corresponding type is first found, and if the corresponding method is not found in the subclass, a lookup is found in the base class.
Format: Class Class_name (Fatherclass):
such as:
class man (object): def __init__ (self,name,sex): self.name=name self.sex=sexclass Student (man): def __init__ (SELF,NAME,SEX,CLASS1): self.name=name self.sex,class1= Class1 at this time student inherited the man this class, man's properties and methods student classes of objects can be called, in addition to private properties and methods.
overriding The
subclass does not want to inherit the method of the parent class intact, but wants to make some modifications, which requires a method rewrite.
For example:
Class man (object): def __init__ (self,name,sex): self.name=name self.sex=sex def speak (self,language): Self.language=language print (language) Class student (man): def __init__ (SELF,NAME,CLASS1): self.name=name self.class1=class1 def speak (self,language): # The Speak method of the subclass overrides the Speak method of the base class printf ("%s was said to be%s", Slef.name, Self.language)
overloaded
Overloading is a means for a class to handle different types of data in a uniform manner. is in a class, the method has the same name, and the parameters are different. What about the return type? can be the same or different.
Features:
(1): variable parameter type
(2): Number of variable parameters
such as:
Class man (object): def __init__ (self,name,sex): self.name=name self.sex=sex def speak (self,language): Self.language=language print ("%s classmate is talking about%s", Slef.name, Self.language) Class student (man): def __init__ (SELF,NAME,CLASS1): self.name=name self.class1= Class1 def speak (Self,language,num): #重载 printf ("%s is talking about%s", Slef.name,self.language)
Multiple Inheritance
For inheritance, Java only supports single inheritance, while Python supports multiple inheritance.
Note: In Python for multiple inheritance, the destructors class has the same method name, and when the subclass is used, the method of that base class is not specified, and then it is looked from left to right in the inheritance relationship.
Format:
Cless class_name (CLASS1,CLASS2,CLASS3 ...)
polymorphic
In object-oriented languages, many different implementations of interfaces are polymorphic. Polymorphism is a technique that allows you to set a parent object to be equal to one or more of his child objects, and after assignment, the parent can operate differently depending on the attributes of the child objects currently assigned to it.
[Python] Learning Basics: Object-oriented Programming