Python multi-inheritance, python inheritance

Source: Internet
Author: User

Python multi-inheritance, python inheritance

Yesterday, my roommate dragged me to eat chicken, so I didn't learn it. I should reflect on it. I only want to learn a little more today, so I will make up for the previous day.

Today I learned more about python inheritance.

I think it is still a little difficult to inherit more. Today I am studying the real-income method of multi-inheritance. I accidentally wrote the name of the constructor of the parent class wrong, And then I encountered a bug. I have been searching for it for a long time, I found that the error was written above, and the error reported by the result editor is far from the one I actually encountered. Therefore, you need to write the code carefully and it is really annoying to find a bug.

Now let's get down to the truth and start the text.

 

 

Class People (object): # This is the first parent class.
Name = ""
Age = 0
_ Weight = 0 # private attribute

Def _ init _ (self, name, age, weight ):
Self. name = name
Self. age = age
Self. _ weight = weight

Def speak (self ):
Print (self. name, self. age, self. _ weight)


# When inheriting a class, the class name is enclosed in brackets and you need to inherit the class. It can be seen that this is a single inheritance.
Class Student (People): # A subclass is defined here and inherits the people class above.
Grade = ""
Def _ init _ (self, name, age, weight, grade ):
People. _ init _ (self, name, age, weight)
Self. grade = grade

Def print_sudent (self ):
Print (self. grade)

# Create another class here

Class Speaker (object ):
Topic = ""
Name = ""
Def _ init _ (self, name, topic ):
Self. name = name
Self. topic = topic

Def print_speak (self ):
Print (self. name, self. topic)

# Here is the main character of today. We can see that the name of the parent class you need to inherit is in the brackets after the class name.
# Note: the sequence of the parent class is very important. For example, you need to call a method that does not exist in the parent class, the python interpreter looks for the method you call from left to right in the parent class.

Class Sample (Student, Speaker ):
A = ""
# This is very important. Here is the method of inheriting the constructor of the parent class.
Def _ init _ (self, name, age, weight, grade, topic, ):
Student. _ init _ (self, name, age, weight, grade)
Speaker. _ init _ (self, name, topic)
Self. a =

# Method Rewriting: if the parent class has this method, but the function cannot meet the requirements of the subclass, then you can define a new function with the same name as the parent class in the subclass,
# When this instance calls this method, it will call the method you override in the subclass.
Def print_speak (self ):
Print (self. name, self. age, self. grade, self. topic, self.)

# Use subclass to generate instances

Test = Sample ("tim", 25, 80, 99, "python", 10)

# Call the method to override a subclass
Test. print_speak ()


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.