A succession brief and basic grammar
# 1. Inheritance is a way to create a new class, in python, the new class can inherit one or more parent classes, parent and can be
# called the base class and super class, the new class is called the derived class and subclass
# python class inheritance is divided into: single and multiple inheritance
2. Why do you want to use inheritance: inheritance party can be used directly inherited the party has some things which are designed to reuse existing code has been improved reusability
class A (Object): Pass # parent class, the base class, the superclass class B: Pass # parent class, the base class, the superclass class A_son (A, B): Pass # subclass, derived class class AB_son (A): Pass # a class can be more than one class inherits # a class can inherit from multiple parent classes only in python in Print (A_son. __bases__ ) Print (AB_son. __bases__ ) Print (A. __bases__ ) # does not inherit the parent class default inherit Object class Animal: DEF the __init__ (Self, name, the aggr, HP): the self.name = name self.aggr = aggr self.hp = hp def eat(self): self.hp += 100 print("回血") class Person(Animal): def __init__(self, name, aggr, hp, sex): Animal.__init__(self, name, aggr, hp) self.hp = hp # 派生属性 class Dog(Animal): def __init__(self, name, aggr, hp, type): # Animal.__init__(self, name, aggr, hp) super().__init__ (name, aggr, the HP) # only new class has, python in all new class self.type of the type = # derived attributes DEF EAT (Self): # Animal.eat (Self) # If you want to achieve both new function, but also want to call the parent class subclass Super () EAT (). self.teeth = 2 DEF Bite (Self, the Person): # derived method person.hp - = self.aggr jin = Dog ( " gold boss " , 100, 500, " Chihuahua " ) alex = the Person ( " Tank " , " 100 " , 600, " Animals " ) Super (Dog, Jin) .eat () # away with the need pass classes and objects outside Print (alex.hp) () jin.eat Print (jin.hp) # parent class attributes that do not, in the sub- appeared, called derived attributes # method of the parent class does not appear in the subclass, called derivative method # as long as the object is a subclass of calls, some subclass name must use the subclass, the subclass only to find there is no parent, if the parent class nor on the error # If the parent class has subclasses, use subclasses # If you want to use the parent class, a separate call to the parent class, needs its own self pass parameters
Second, inheritance and abstract
Abstract: not specific, unclear, vague, not read
The same portion of the plurality of process subclasses, extraction, to form a new class, a process also known as abstract
The proper use of inheritance:
1. The first abstract inheritance
2. inherited an already existing class, extend or modify the original function
# Extracting the same portions of the teachers and students form class person class the Person: DEF the __init__ (Self, name, Age, Gender): the self.name = name self.age = Age self.gender = Gender DEF say_hi (Self): Print ( " name:% S, Gender:% S, Age:% S " % (self.name, self.gender, self.age)) class teacher (the Person): DEF Teaching (Self): Print ( " teachers teach the students, writing code .... " ) T1 = Teacher ( " Jack","male",20) t1.say_hi() class Student(Person): pass stu1 = Student("rose","female",18) stu1.say_hi()
Third, property search order
# class A: # text = "haha" # # class B(A): # text = "heihei" # pass # # b = B() # print(b.text) # heihei
The object itself -> where the class -> find parent -> parent class parent -> Object
Fourth, the derivation and covering
Derived: that there is no parent, the child had called derived class
Cover: also referred to rewrite overrides, subclasses appears when fully consistent with the method of the parent class name attribute or
class Person: def say_hi(self): print("hello") class Student(Person): def say_hi(self): print("hello world!") stu = Student() stu.say_hi()
Fifth, the subclass call the parent class
class the Parent: text = " abc " DEF say_something (Self): Print ( " Anything " ) class Sub (the Parent): DEF show_info (Self): # Print (Super (Sub, Self) .text) # Super (Sub, Self ) .say_something () # new syntax for accessing mode 2 py3 the most common way to Print (Super (). text) Super (). say_something () # ways to directly specify the class name to call 3 # Print (Parent.text) # the Parent. say_something (Self) # Sub Sub = () # sub.show_info ()
Sixth, the initialization method must be called
# Why initialization method is called the parent class Init method of class the Person: DEF __init__ (Self, name, Gender, Age, * args): self.name = name self.gender = Gender self.age = Age self.aa ( ) DEF AA (Self): Print ( " AA RUN " ) DEF say_hi (Self): Print ( " name:% S, Gender:% S, Age:% S " % (the self.name, self.gender, Self. Age)) class Student (the Person): DEF __init__(self,name,gender,age,number): super().__init__(name,gender,age) self.number= number def say_hi(self): super().say_hi() print("numnber:%s" % self.number) stu = Student("rose","mael",20,"old01") stu.say_hi() # 如果子类
Seven, can limit the list of elements to achieve a type
Needs to achieve a can limit the element type of a list of classes "" " class MyList (List): DEF __init __ (Self, element_type): Super () .__ the init __ () # call the initialization method of the parent class to complete basic initialization self.element_type = element_type DEF append (Self, Object): "" " : param Object: to store the elements : return : no " "" IF of the type (Object) == self.element_type: # we need here append function to access the parent class to accomplish the actual storage operation Super (MyList, Self) .append (Object) the else: Print ( "SIR Sorry, you element of the type not iS% S"% self.element_type) # create the element type is specified to be stored m = MyList (int) # when you have demand,Thing is the need to do something it thought the initialization method when creating objects m.append(1) print(m[0]) m.append("121212")
#### stressed stressed:
When you inherit an existing class, and you override the parent class init method, you must call the parent class initialization method of the first line of the initialization method, passing in the required parameters parent class