"Python Learning note-OOP" Learning Inheritance (inheritance) and composition (composition) in an instance

Source: Internet
Author: User

"Three main features of object-oriented ":

  Encapsulation (encapsulation): Encapsulates data and methods through abstract classes. The data for the instance can only be obtained or manipulated through the public interface (that is, the method of the instance).

"Pros": 1. Functions need to be defined in one place, without having to repeat them in many places

2. Ensure the security of the data inside the object

3. When we want to use a method, we just need to know what we are going to return with this method, without needing to know what the internals are doing to achieve it.

It's like we want to change the channel when we watch TV, just press a few buttons, don't need to know how to FM.

  "Inheritance (Inheritance)" : Simple to understand, for example, we built a class, we first called the Parent class (Super Class);

Then we want to build several other classes that want to use some of the attributes that are similar to those in the parent class, such as attributes and methods in the parent class;

Then we can do it by inheritance, and these classes can be called subclasses (sub Class).

Subclasses can inherit all the properties and methods of the parent class

When querying a property, the order is: instance, class, parent class, parent class ...

Multiple inheritance: A subclass can inherit multiple parent classes, which involves the question of the order of the property queries:

Queries in Python Use the DFS algorithm, but when duplicate objects occur, the objects that appear earlier are deleted in the query order list.

We can query a class's MRO (method resolution Order) by using CLS.MRO (). (CLS is replaced with any class name that wants to query MRO)

Example: (1) Class D's parent classes are Class B and C, which we represent as Class D (B,C); The parent class of B is a that is Class B (a).

Then D.mro () is d-b-a-c, that is, when we execute the D instance, we first query in D, then B, then a, then C.

(2) When there is more than one parent inheriting the same class, in order to prevent ambiguity, when duplicate classes appear in the MRO, the previous repeating class is removed in the MRO:

For example: Class D (b,c), Class B (a), Class C (a):, then the MRO for D is: d-b-c-a

The DFS steps are as follows: [d]-->[d,b]-->[d,b,a]-->[d,b,a,c]--> (the parent class of C is a, which already appears in front, remove the previous a)-->[d,b,c,a]

  Polymorphic (polymorphism): Simple to understand, different classes have the same method name (methods name), that is, there is the same interface, we can call the same method name in any instance of a class.

But in fact, although these methods have the same name and are conceptually similar, internal operations are different.

For example, integers and strings have the __add__ () method, that is, we can use 1+2, ' 1 ' + ' 2 ', but the result is 1+2-->3, ' 1 ' + ' 2 '--' 12 ', integer implementation is the addition, string implementation is stitching.

"Composition": several different classes can work together, but there is no dependency between them (decoupling); This is a point where it differs from inheritance.

--and more guarantees the maintainability of the code.

"Instance": Create three classes with inheritance or composition: WriteFile (), txtfile (), CSVFile () write file operations. where txtfile () writes datetime and a string of information to the TXT file, csvfile with "," separates the different values and then enters.

The first is: by inheriting the way to achieve

  

Results:

In the Text1.txt:

In the Text1.csv:

    

The second kind: By combining the way to achieve

Results:

In the Text2.txt:

    

In the Text2.csv:

    

"Reference course" Https://www.udemy.com/python-beyond-the-basics-object-oriented-programming/learn/v4/overview

"Python Learning note-OOP" Learning Inheritance (inheritance) and composition (composition) in an instance

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.