1 Inheritance
'''
1. What is inheritance?
Inheritance in a program is a method for creating a subclass. The newly created class is called subclass \ derived class, And the inherited class is called parent class \ base class \ super class.
Inheritance describes a genetic relationship. Child classes can reuse attributes of parent classes.
2. Why inherit?
Reduce code redundancy between classes
3. How to inherit
Abstraction before inheritance
'''
# What are the features inherited in Python? Single inheritance & multi-Inheritance
# Class parent1 (object ):
# Pass
#
# Class parent2:
# Pass
#
# Class sub1 (parent1 ):
# Pass
#
# Class sub2 (parent1, parent2 ):
# Pass
#
# Print (sub1. _ bases _) # inherit who will output
# Print (sub2. _ bases __)
#
# Print (parent1. _ bases __)
# Print (parent2. _ bases __)
# X = 2
# Class FOO:
# X = 1
# Pass
# OBJ = Foo ()
# Obj. x = 3
# Print (obj. X)
# Differences between python2 and python3 in inheritance
# New Class: any new class that inherits the subclass of the object class and its sub-subclass...
# Classic class: the class does not inherit the subclass of the object class, and the sub-subclass of this subclass is called a classic class...
# Only in python2 can the new and classic classes be distinguished
# What is the difference between a new class and a classic class in attribute search ???
2 solve Class and Class Code redundancy issues based on inheritance
# How to reuse the function of the parent class in the new function derived from the subclass:
# Method 1: access a function in a class by name. It has nothing to do with inheritance.
Class oldboypeople:
School = 'oldboys'
Def _ init _ (self, name, age, gender ):
Self. Name = Name
Self. Age = Age
Self. Gender = gender
# Print (oldboypeople. _ init __)
Class oldboystudent (oldboypeople ):
# Def _ init _ (self, name, age, gender ):
# Self. Name = Name
# Self. Age = Age
# Self. Gender = gender
Def choose_course (Self ):
Print ('% s is choosing course' % self. Name)
Class oldboyteacher (oldboypeople ):
# Tea, 'egon', 18, 'male', 10,300 0
Def _ init _ (self, name, age, gender, level, salary ):
# Self. Name = Name
# Self. Age = Age
# Self. Gender = gender
Oldboypeople. _ init _ (self, name, age, gender)
Self. Level = level
Self. Salary = salary
Def score (self, Stu, num ):
Stu. num = num
Print ('instructor % s scores % s' % s for students (self. Name, Stu. Name, num ))
# Stu = oldboystudent ('kevin ', 38, 'male') #__ init ___ (stu1, 'kevin', 38, 'male ')
# Print (STU. _ dict __)
Tea = oldboyteacher ('egon', 18, 'male',) #__ init ___ (tea, 'egon', 18, 'male)
Print (tea. _ dict __)
# Print (STU. School)
# Print (tea. School)
3
# In the context of single inheritance, the search sequence for new classes and classic classes is the same
# Obj-> class-> parent class->...
# Class FOO:
# Def F1 (Self ):
# Print ('foo. f1 ')
#
# Def F2 (Self ):
# Print ('foo. f2 ')
# Self. F1 () # obj. F1 ()
#
# Class bar (FOO ):
# Def F1 (Self ):
# Print ('bar. f1 ')
#
# OBJ = bar ()
# Obj. F2 ()
# In the context of multi-inheritance, if a subclass inherits multiple branches, but multiple branches do not converge to a non-object class, the search sequence for new classes and classic classes is the same:
# Search for a branch from left to right
Class E:
# Xxx = 'E'
Pass
Class F:
# Xxx = 'F'
Pass
Class B (e ):
# Xxx = 'B'
Pass
Class C (f ):
# Xxx = 'C'
Pass
Class D:
# Xxx = 'D'
Pass
Class A (B, c, d ):
# Xxx = 'A'
Pass
OBJ = ()
# Obj. xxx = 111
# Print (obj. XXX)
Print (A. Mro ())
# In the context of multi-inheritance, if a subclass inherits multiple branches, but multiple branches eventually converge to a non-object class (diamond Inheritance Problem)
# New category: Search for breadth first: obj-> A-> B-> E-> C-> F-> D-> G-> Object
# Classic category: Deep Priority Search: obj-> A-> B-> E-> G-> C-> F-> d
# Class G:
# Xxx = 'G'
#
# Class E (G ):
# Xxx = 'E'
# Pass
#
# Class F (g ):
# Xxx = 'F'
# Pass
#
# Class B (e ):
# Xxx = 'B'
# Pass
#
# Class C (f ):
# Xxx = 'C'
# Pass
#
# Class D (G ):
# Xxx = 'D'
# Pass
#
# Class A (B, c, d ):
# Xxx = 'A'
# Pass
#
#
# Print (A. Mro ())
4 super Method
# How to reuse the function of the parent class in the new function derived from the subclass:
# Method 1: access a function in a class by name. It has nothing to do with inheritance.
# Method 2: Super (oldboyteacher, self). In python3, super can get a special object by calling this function without passing parameters, this object is used to access attributes in the parent class,
# Emphasis: Super will strictly refer to the Mro list of the class to search for attributes in sequence.
# Class oldboypeople:
# School = 'oldboys'
# Def _ init _ (self, name, age, gender ):
# Self. Name = Name
# Self. Age = Age
# Self. Gender = gender
#
# Class oldboyteacher (oldboypeople ):
# Tea, 'egon', 18, 'male', 10,300 0
# Def _ init _ (self, name, age, gender, level, salary ):
# Oldboypeople. _ init _ (self, name, age, gender)
# Super (oldboyteacher, self). _ init _ (name, age, gender)
#
# Self. Level = level
# Self. Salary = salary
#
# Def score (self, Stu, num ):
# Stu. num = num
# Print ('instructor % s scores % s' % s for students (self. Name, Stu. Name, num ))
#
# Tea = oldboyteacher ('egon', 18, 'male',) #__ init ___ (tea, 'egon', 18, 'male)
# Print (tea. _ dict __)
# Print (STU. School)
# A does not inherit B,
Class:
Def test (Self ):
Print ('a. test ')
Super (). Test ()
Class B:
Def test (Self ):
Print ('from B ')
Class C (A, B ):
Pass
C = C ()
C. Test ()
Print (C. Mro ())
2018.10.23