Python3 study Notes object oriented; process; class

Source: Internet
Author: User

Programming Paradigm

Programming is a programmer with a specific syntax + data structure + algorithm composed of code to tell the computer how to perform the task of the process, a program is a programmer in order to get a task result and a set of instructions, is so-called all roads to Rome, the way to achieve a task there are many different ways, The types of programming that are summed up by the characteristics of these different programming methods are the programming paradigm. Different programming paradigms essentially represent different solutions to various types of tasks, and most languages support only one programming paradigm, and of course some languages can support multiple programming paradigms at the same time. Two of the most important programming paradigms are process-oriented programming and object-oriented programming.

Process-oriented programming (procedural programming)
Procedural programming uses a list of instructions to tell the computer what to do step-by-step.
Process-oriented programming dependencies-you guessed-procedures, a procedure contains a set of steps to be computed, and the process is called Top-down languages, which is the procedure from the top to the next step, step by step from top to bottom, to solve the problem from beginning to end. The basic design idea is that the program starts with solving a big problem, and then breaks down a big problem into many small problems or sub-processes, and the process continues to decompose until the small problem is simple enough to be solved within a small step.

If you set a variable at the beginning of the value of 1, but if other sub-procedures rely on the value of 1 of the variable to function properly, if you change this variable, then you have to modify the sub-process, if there is another subroutine dependent on the sub-process, it will have a series of effects, as the program is getting larger, This way of programming is more difficult to maintain.
So we generally think that if you just write some simple scripts to do some one-off tasks, it's great to use a process-oriented approach, but if the task you're dealing with is complex and needs to be iterative and maintainable, it's the most convenient way to use object-oriented.

Object-Oriented Programming

OOP programming is the use of "class" and "object" to create a variety of models to achieve a real-world description, the use of object-oriented programming because it can make the maintenance and extension of the program easier, and can greatly improve the efficiency of program development, in addition, An object-oriented program can make it easier for people to understand your code logic and make team development easier.

Several core features of object-oriented are as follows

Class
A class is an abstraction, a blueprint, a prototype for a class of objects that have the same properties. The properties of these objects are defined in the class (variables (data)), common methods

Object objects
An object is an instantiated instance of a class, a class must be instantiated before it can be called in the program, a class can instantiate multiple objects, each object can also have different properties, like human refers to everyone, each person refers to the specific object, people and people before there is a common, there are different

Encapsulation Package
The assignment of data in a class, internal invocation is transparent to external users, which makes the class A capsule or container in which the data and methods of the class are contained.

Inheritance inheritance
A class can derive subclasses, properties, methods defined in the parent class, automatic quilt class inheritance

Polymorphism Polymorphism
Polymorphism is an important feature of object-oriented, simple point: "An interface, a variety of implementations", refers to a base class derived from a different subclass, and each subclass inherits the same method name, but also the parent class method to do a different implementation, this is the same thing shows a variety of forms.
Programming is actually a process of abstracting the concrete world, which is a manifestation of abstraction, abstracting the common denominator of a series of specific things, and then through this abstract thing, and dialogue with different concrete things.
Sending the same message to different classes of objects will have different behavior. For example, if your boss lets all employees start working at nine o'clock, he says "get started" at nine o'clock, instead of saying to the salesperson: "Start a sales job," say to the technician, "Start technical work", because "employee" is an abstract thing, so long as the employee can start to work, He knows that. As for each employee, of course, they do their job and do their jobs.
Polymorphism allows the object of a subclass to be used as an object of the parent class, a reference to a parent type to an object of its subtype, and the method called is the method of that subtype. The code that references and invokes the method is already determined before it is compiled, and the object pointed to by the reference can be dynamically bound during run time

To define a class:

class Student (object):     Pass

class names are followed by classes, that is, the Student class name is usually the beginning of the word, followed by the class is the class (object) from which to inherit, the concept of inheritance we will say later, usually, if there is no appropriate inheritance class, the use of object classes, This is the class that all classes will eventually inherit.

instantiation : The creation of an instance is implemented through the class name + ():

Bart = Student ()

Because a class can act as a template, you can force a number of attributes that we think must be bound to be filled in when creating an instance. By defining a special __init__ method, when you create an instance, you bind the name score attributes, such as:

class Student (object):     def __init__ (self, Name, score):         = name        = Score

Note that __init__ the first parameter of the method is always the one that self represents the created instance itself, so __init__ within the method, you can bind various properties to it self , because self it points to the created instance itself.

With the __init__ method, when you create an instance, you cannot pass in an empty argument, you must pass in a __init__ parameter that matches the method, but it self does not need to be passed, and the Python interpreter will pass the instance variable in itself.

Access Restrictions:

If you want the internal properties to be inaccessible externally, you can add the name of the property with two underscores __ , and in Python, the variable name of the instance __ becomes a private variable (private), which can only be accessed internally and cannot be accessed externally, so Let's change the student class.

class Student (object):     def __init__ (self, Name, score): Self        . __name = name self        . __score = Score

After the change, for external code, there is no change, but it has been unable to access from outside 实例变量.__name and 实例变量.__score :

What if external code gets name and score? You can add to the student class get_name and get_score this method:

class Student (object):    ...     def get_name (self):         return self. __name    def Get_score (self):         return self. __score

What if I want to allow external code to modify score? You can add additional methods to the student class set_score :

class Student (object):    ...     def Set_score (self, score): Self        . __score = Score

Inherited

In OOP programming, when we define a class, we can inherit from an existing class, and the new class is called a subclass (subclass), and the inherited class is called the base class, the parent class, or the superclass (base-Class, Super-Class).

classpeple (object):def __init__(self,name,age): Self.name=name Self.ane= AgedefEating (self):Print('%s is eating'%self.name)classMakefriends (object):defmakefriends (self, body):Print('%s is makefriends with%s'%(Self.name, body.name))classMan (peple,makefriends):def __init__(Self,name,age,money):#Add new ParametersSuper (Man,self).__init__(Name,age)#equivalent to peple.__init__ (self,name,age) #新式类的写法Self.money=money

Python2 Classic class inherits by depth precedence, new class inherits by breadth precedence

Python3 Classic class and new class are inherited by breadth precedence

Python3 study Notes object oriented; process; class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.