6-python Automation-day06-Object-oriented

Source: Internet
Author: User

This section: Object-Oriented Programming explains why you should use object-oriented development? Object-oriented features: encapsulation, inheritance, polymorphic classes, methods. Reprinted: Http://www.cnblogs.com/alex3714/articles/5188179.html http://www.cnblogs.com/alex3714/articles/5213184. HTML Object-oriented 3 features: encapsulation (the implementation details of some features are not exposed to the external)

The package is best understood. Encapsulation is one of the object-oriented features and is the main feature of object and class concepts.

Encapsulation, which is the encapsulation of objective things into abstract classes, and classes can put their own data and methods only trusted class or object operation, to the untrusted information hiding.

Inheritance (reuse of code)

One of the main functions of object-oriented programming (OOP) language is "inheritance". Inheritance refers to the ability to use all the functionality of an existing class and to extend these capabilities without rewriting the original class.

New classes created through inheritance are called "subclasses" or "derived classes."

The inherited class is called the base class, the parent class, or the superclass.

The process of inheritance is from the general to the special process.

To implement inheritance, it can be implemented through inheritance (inheritance) and combination (composition).

In some OOP languages, a subclass can inherit multiple base classes. However, in general, a subclass can have only one base class, and to implement multiple inheritance, it can be implemented by multilevel inheritance.

There are 2 main implementations of the concept of inheritance: implementation inheritance, interface inheritance.

?         Implementation inheritance refers to the ability to use the properties and methods of a base class without additional coding; Interface inheritance is the ability to use only the names of properties and methods, but the subclass must provide the implementation (subclass refactoring method), and one thing to consider when using inheritance is that the relationship between the two classes should be a "belongs" relationship. For example, the Employee is a person and the Manager is a person, so these two classes can inherit the People class. But the Leg class cannot inherit the person class, because the leg is not a human. Abstract classes define only the generic properties and methods that will be created by the subclass.

OO development paradigm is roughly: dividing objects → abstract classes → organizing classes into hierarchical structures (inheritance and compositing) → Designing and implementing several stages with classes and instances.

Single inheritance and multiple inheritance: one of the py2 classical classes is inherited by the depth first, the new class is inherited by the breadth first----Py3 Classic class and modern class are uniformly inherited by breadth first

code example: Super (inherited class name, self). __INIT__ (variable)

Polymorphism (interface reuse, an interface, multiple implementations)Polymorphism (POLYMORPHISN) is a technique that allows you to set a parent object to be equal to one or more of his child objects, after which the parent object can operate differently depending on the attributes of the child object currently assigned to it. To put it simply, it is a sentence: A pointer to the parent class type is allowed to be assigned a pointer to the child class type. So what is the role of polymorphism? We know that encapsulation can hide implementation details and make code modular; Inheritance can extend existing code modules (classes); they are all designed to-code reuse. And polymorphism is for another purpose--interface reuse! The role of polymorphism is to ensure that classes are called correctly when inheriting and deriving a property of an instance of any class in the family tree.  Pyhon Many grammars support polymorphism, such as Len (), sorted (), you send Len a string to return the length of the string, and the list returns the length of the list.

6-python Automation-day06-Object-oriented

Related Article

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.