Python Object-oriented inheritance

Source: Internet
Author: User

Inheritance: Single inheritance and multiple inheritance1. Initial knowledgeclassFather (object):#If you do not write object default inheritance    PassclassSon (Father):#inside the parentheses are called: Subclasses or derived classes outside of the parent class or the base class or the superclass bracket    PassclassAnimal:breath='Breathing'    def __init__(self, name, sex, age): Self.name=name Self.sex=Sex Self.age= AgedefEat (self):Print(self)#111 #<__main__. Person object at 0x111ac3710>        Print('animals all need to eat ....')classPerson (Animal):PassPrint(Person.breath)#BreathingPerson.eat (111)#animals all need to eat ....P1= Person ('Jake','male', 20)Print(P1.breath)#BreathingPrint(p1)#<__main__. Person object at 0x111ac3710>P1.eat ()#animals all need to eat ....Result: Breathing111animals all need to eat .... Breathing<__main__. Person object at 0x111ac3710> <__main__. Person object at 0x111ac3710>animals all need to eat ....    Summary: Subclasses and subclasses instantiate objects that can access any of the methods and variables 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 of the parent class are executed: Subclasses do not define methods that have the same name as the parent class to execute only subclasses: Create this method in a subclass. Both the method of the subclass and the method of the parent class are implemented. There are two workarounds.1. Pass the class name. 2. Through the super () function1,animal.__init__(self, name, sex, age)2,super ().__init__(name,sex,age)classAnimal:breath='Breathing'    def __init__(self, name, sex, age): Self.name=name Self.sex=Sex Self.age= Agedefeat (Self, argv):Print('%s Eat%s'%(Self.name, argv))classPerson (Animal):def __init__(self, name,sex,age,wing):#animal.__init__ (self, name, sex, age)Super ().__init__(Name,sex,age)#Super (Brid,self) __init__ (name,sex,age)Self.wing =WingdefEat (SELF,ARGV): Super (). Eat (argv)Print('eat ...') P1= Person ('Parrot','male', 20,'Green Wings')Print(P1.__dict__) P1.eat ('Jinchan') Result: {'name':'Parrot','Sex':'male',' Age': 20,'Wing':'Green Wings'The parrot eats Jinchan to eat ...2. Advanced Class: Classic class, New Class modern class: All inherited object class is a new class.             Python3x All classes are new classes, because classes in python3x inherit object by default. Classic class: The object class does not inherit the classic class python2x: (both the new class and the classic Class) All classes do not inherit the object class by default.     All classes are classic classes by default. You can let it inherit object. Single inheritance: New classes, the Classic class query order. Multiple inheritance: New class: Follow breadth first. Classic class: Follow the depth first. Multi-inheritance of the new class breadth first: One way to the penultimate level, judge, if the other road can reach the end, then go back to the other road. If not, go to the finish line. Classic class depth with multiple inheritance first: one way to go to the end.

Python Object-oriented inheritance

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.