18th Day of the Python learning Journey

Source: Internet
Author: User

Three main characteristics of object-oriented: inheriting polymorphic encapsulation

Initial knowledge Inheritance:

Subclasses and subclasses instantiate objects that can call any method and variable of the parent class

The class name can access all the contents of the parent class

Objects instantiated by subclasses can also access all the contents of the parent class

Only the methods in the parent class are executed: Do not define the same method as the parent class in the child class

Methods that execute only subclasses: Create this method in a subclass

To execute a method in a child class and a parent class:

1. Passing arguments to a constructor in the parent class

2. Using Super

Class Animal:    def __init__ (self,name,age,sex):        self.name=name        self.age=age        self.sex=sex    def Eat (self):        print ("%s Meat"%self.name) class Cat (Animal):    def __init__ (self,name,sex,age,color):        # Animal. __init__ (Self,name,sex,age)        super (). __init__ (name,age,sex)        self.color=color    def Eat (self):        Print ("%s%s catch Mouse"% (self.color,self.name))        super (). Eat () C=cat ("Jen", ' Male ', 3, ' White ') c.eat ()

  

The advanced order of inheritance

Inheritance: Single Inheritance multiple inheritance

Class: Classic Class New Class

All classes in Python3 are new classes, which inherit the object class by default

Classic class: Classes that do not inherit object by default are classic classes (both classic and modern in Python2) all classes do not inherit the object class by default

Single inheritance: New class Classic class query Order consistent

Multiple inheritance: New class: Follow breadth First

Classic class: Follow depth First

Breadth First: A road to the penultimate level, judge, if the other road can go to the end, then go back to the other road, if not, go to the end

Depth first: One way to go to the end

18th Day of the Python learning Journey

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.