Python Basic---polymorphism and binding method

Source: Internet
Author: User


First, abstract class

1, the definition of abstract class: Abstract the same content from a bunch of classes, and re-form a new class, such classes belong to the abstract class.

2, Banana class is a kind of fruit, Apple is a kind of fruit, grape is a kind of fruit, but they belong to the fruit, from these classes can be abstracted from the same parts are belong to the fruit category, this fruit category belongs to abstract class. But you can never eat an object called fruit, so an abstract class cannot be instantiated, he can only be inherited by another class, and the subclass must implement an abstract method.

3. The abstract class is used in conjunction with the ABC module. (previously read and write)

4, the essence of abstract class or class, refers to a group of similar places, including data properties and function properties, and the interface only emphasizes the similarity of function properties. Second, polymorphic

1, what is polymorphic?

Polymorphism refers to a variety of forms of things, an abstract class has more than one sub-class, so the concept of polymorphism depends on inheritance. (Water is an abstract class, he can have steam, spring, ice and many other forms.) )

2, the sequence type has many forms, is the string, the list, the tuple, therefore they all follow this sequence type abstract class use method, May through the index value can fragment and so on.

Iii. polymorphism

1. What is polymorphism?

Polymorphism means that functions or objects with different functions can use the same function names, so that functions with different functions can be invoked using the same function name. :

    

2, polymorphism is divided into static polymorphism and dynamic polymorphism two kinds of.

Static polymorphism: Data of any sequence type can be calculated by +

    

Dynamic polymorphism: An example of

3, polymorphism refers to a function to implement different functions or objects function.

4. What are the benefits of polymorphism?

4.1 Increase the flexibility of the program, status quo, regardless of how the object changes we can call it in the same form.

4.2 The scalability of the program is increased, and the user can create a new object, but the method of invocation does not change. Iv. binding methods and non-binding methods

1, the functions defined in the class are divided into two categories, one is a function with a binding method, and one is a function without a binding method.

2. Functions with binding methods

2.1 Methods that are bound to an object: A function property defined in a class is bound to an object by default, who is bound to it, and is automatically called by the bound person as

The first parameter is passed in.

2.2 Methods that are bound to classes: functions decorated with classmethod adorners in classes are bound to classes. Whether a class call or an object call will automatically

As the first parameter, it is automatically passed in.

3, the function of the non-binding method.

Functions in a class that are decorated with staticmethod adorners are bound. Both the object and the class can call it, but the value is not automatically passed.

4. Eg:

  

    

Python Basic---polymorphism and binding method

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.