[Python study notes, python Study Notes

Source: Internet
Author: User

[Python study notes, python Study Notes

[Three features of object-oriented]:

  [Encapsulation )]: Encapsulate data and methods through abstract classes. The instance data can only be obtained or operated through the public interface (that is, the instance method.

[Advantages]: 1. The function can be defined only in one place, but not in many places.

2. ensure the security of the data inside the object

3. When we want to use a method, we only need to know what results will be returned using this method, and do not need to know what operations are actually performed internally.

As if we want to change the channel when watching TV, we only need to press a few buttons and do not need to know how to adjust the frequency.

  [Inheritance )]: Simple understanding. For example, if we create a class, we first call it the parent class );

Then we want to create several other classes. These classes want to use features similar to the parent class, such as attributes and methods in the parent class;

We can achieve this through inheritance. These classes can be called sub classes ).

Subclass can inherit all attributes and methods of the parent class

When querying an attribute, the order is: instance, class, parent class, parent class of the parent class...

[Multi-inheritance]: A subclass can inherit multiple parent classes, which involves the order of attribute query:

In python, the dfs algorithm is used to query objects. However, when duplicate objects exist, the objects that have appeared before are deleted from the query order list.

We can use cls. mro () to query the MRO (method resolution order) of a class ). (Replace cls with any class name that you want to query mro)

Example: (1) the parent class of class D is class B and C. We represent class D (B, C). The parent class of B is class A, that is, class B ().

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

(2) when more than one parent class inherits the same class, in order to prevent ambiguity, when a duplicate class appears in MRO, remove the previous duplicate class in mro:

For example: class D (B, C), class B (A), class C (A):, then the MRO of D is: D-B-C-A

The dfs procedure is as follows: [D] --> [D, B] --> [D, B, A] --> [D, B,, c] --> (the parent class of C is A, which has already appeared before. Remove the parent class A) --> [D, B, C, A]

  [Polymorphism )]: In simple terms, different classes have the same method name, that is, the same interface. We can call the same method name in any class instance.

However, although these methods have the same name and are similar in concept, internal operations are different.

Composition: several different classes can work together, but there is no dependency between them (decoupling); this is a difference between them.

--> This ensures code maintainability.

[Instance]: creates three classes using inheritance or combination: WriteFile (), TXTFile (), and CSVFile () to write files. TXTFile () writes the date and time and a string of information into the txt file. CSVFile is separated by "," and then input.

 

First: implemented through inheritance

  

Result:

In text1.txt:

In text1.csv:

    

Second: implementation through combination

Result:

In text2.txt:

    

In text2.csv:

    

Https://www.udemy.com/python-beyond-the-basics-object-oriented-programming/learn/v4/overview (reference course)

 

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.