Inherited
# One of the three main features of object-oriented: inheritance
# Inheritance: There are at least two classes: what is the relationship of what, in order to avoid having the same code between several classes
# Parent class: Animal
# sub-category: Dog person
# Animal
Example
# Two types in Python: Classic and Modern
# Python3 is only the new class---are inherited by default Object,class Animal (object) = = Class Animal:
# Python2 Classic class and new class coexist
# Class Animal:-- is the classic class
# class Animal (object):---> New class
# class ParentClass1: #定义父类
# Pass
#
# class ParentClass2: #定义父类
# Pass
#
# class SubClass1 (PARENTCLASS1): #单继承, base class is ParentClass1, derived class is subclass
# Pass
#
# class SubClass2 (PARENTCLASS1,PARENTCLASS2): #python支持多继承, separate multiple inherited classes with commas
# Pass
# subclass1.__bases__ #__base__只查看从左到右继承的第一个子类, __bases__ is looking at all inherited parent classes
Summary:
# Two classes have the same code
# Inheritance: The same code is placed in the parent class, and the object of the child class is not found in the subclass, and the parent class is used
# single inheritance and multiple inheritance
# The parent class is also called a superclass or base class
# subclasses are also referred to as derived classes
# Abstract and inherited relationships: first abstraction and inheritance
classAnimal:def __init__(self, name,aggressivity,life_value): Self.name=name Self.aggressivity=aggressivity Self.life_value=Life_valuedefEat (self): Self.life_value+=10classDog (Animal):def __init__(self, name, breed, aggressivity, Life_value): Animal.__init__(self,name,aggressivity,life_value) self.breed= Breed#Derived property: Property not in parent class defBite (Self,people):#Derivation method: Method not used by parent classPeople.life_value-=self.aggressivity#peopleclassPerson (Animal):def __init__(self, name, aggressivity, Life_value, money):#animal.__init__ (Self,name,aggressivity,life_value)Super ().__init__(Name,aggressivity,life_value)#New ClassSelf.money = Money#Derived property: Property not in parent class defAttack (Self,dog):#Derivation method: Method not used by parent classDog.life_value-=self.aggressivitydefGet_weapon (self,weapon_obj):ifSelf.money >Weapon_obj.price:self.money-= Weapon_obj.price#boss Kim buys weapons .Self.weapon = Weapon_obj#Kim's boss is equipped to play dog sticksSelf.aggressivity + = Weapon_obj.aggr#Kim's boss has increased his attack.Snoopy= Dog ('Taibai','BEIJING-Pakistan', 250,500)Print(Snoopy.name)Print(Snoopy.breed) snoopy.eat ()Print(Snoopy.life_value) Super (dog,snoopy). Eat ()#Animal.eat (Snoopy)Print(Snoopy.life_value)
Derived property: Use the Init method of the parent class in its own Init method---Call the method by naming the names
# Derivation Method: Add a parent class to a subclass
# as long as the subclass has, use the properties or methods of the child class
# as long as you want to use the parent class, use the Animal.eat (Snoopy) parent class name. method of the parent class (subclass object) ---This is the calling method in the 2.7 classic class
# in the new class you need to use Super Method Super (subclass name, subclass object). The parameter of super can be omitted in the method name () class -This is the one in the new class
# Use the subclass's object to invoke the parent class's method:
# If there is no such method in the subclass, use the parent class directly
# If the subclass has a method with the same name as the parent class:
# Classic class: Name calls class names. Method name (subclass object) inside and outside the class consistent
# New class: Use Super method, super (subclass name, subclass object). The parameter of super can be omitted in the method name () class
Interview questions:
Diamond Succession Issues
#The environment in this section is in python2.7.#multiple inheritance problems for classical and modern classes, inheritance order problems#Classic class: Broad and profound, so the classic class is depth first, go deep to find#New class: Breadth FirstclassF (object):Pass deff (self):Print('F')classE (F):Pass deff (self):Print('E')classD (F):Pass #def f (self): #print (' D ')classB (D):Pass #def f (self): #print (' B ')classC (E):Pass deff (self):Print('C')classA (b,c):Pass #def f (self): #print (' A ')a=A () a.f ()Print(A.mro ())#New class: View inheritance Order#class A (object):p the #新式类#py3--Breadth First#py2--New Class#interview--can correspond to the new class is breadth first Classic class is depth first
Polymorphic
#Python does not support polymorphicclassAnimal:PassclassPerson (Animal):defAttack (self):PassclassDog (Animal):defAttack (self):PassdefAttack (obj):# polymorphicObj.attack () d=Dog () p=Person () attack (d)#D.attack ()Attack (P)#P.attack ()Print(10)#Duck Type list tuple is a pair of duck types#List#Meta-group#slices: string list tuples#+: String List numberdefLen (L):Pass
python-inheritance and inheritance problems and polymorphism