1 inheritance
Concept of inheritance: inheritance is a way to create new classes, in Python, a new class can inherit from one or more parent classes, the original class is called a base class or a superclass, and a new class is called a derived class or subclass.
The inheritance of classes in Python is divided into: single inheritance and multiple inheritance
The inheritance syntax is as follows:
Class ParentClass1: #定义父类
Pass
Class ParentClass2: #定义父类
Pass
Class SubClass1 (PARENTCLASS1): #单继承, the base class is ParentClass1, and the derived class is subclass
Pass
Class SubClass2 (PARENTCLASS1,PARENTCLASS2): #python支持多继承, separate multiple inherited classes with commas
Pass
To view inherited methods
__bases__
>>> subclass1.__bases__
(<class ' __main__. ParentClass1 ';,)
>>> subclass2.__bases__
(<class ' __main__. ParentClass1 ';, <class ' __main__. ParentClass2 ' >)
If you do not specify a base class, the Python class inherits the object class by default, object is the base class for all Python classes, and it provides implementations of some common methods, such as __str__.
>>> parentclass1.__bases__
(<class ' object ';,)
>>> parentclass2.__bases__
(<class ' object ';,)
1.1 Inheritance and abstraction
Abstraction is the extraction of a similar or more like part.
An abstraction is divided into two levels:
1. Take the parts of Barack Obama and Lionel Messi, which are more like the two, into categories;
2. Extract the three categories of people, pigs, and dogs into a parent class.
The main function of abstraction is to classify categories (which can isolate concerns and reduce complexity).
Inheritance: is based on abstract results, through the programming language to achieve it, it must be through the process of abstraction, in order to express the abstract structure through inheritance.
Abstraction is just the process of analysis and design, an action or a skill that can be obtained by abstraction.
1.2 Inheritance and re-usability
========================= First part
For example
Cats can: Meow meow, eat, drink, pull, sprinkle
Dogs can: bark, eat, drink, pull, sprinkle
If we were to create a class for both cats and dogs, then we would need to implement all of their functions for cats and dogs, pseudocode as follows:
#猫和狗有大量相同的内容
Class Cat:
def meow Meow (self):
print ' Meow meow '
Def eat (self):
# do something
def drink (self):
# do something
def pull (self):
# do something
def scatter (self):
# do something
Class Dog:
def barking (self):
print ' Meow meow '
Def eat (self):
# do something
def drink (self):
# do something
def pull (self):
# do something
def scatter (self):
# do something
In the process of developing a program, if we define a Class A and then want to create another class B, but most of the content of Class B is the same as Class A
It is not possible to write a class B from scratch, which uses the concept of class inheritance.
Create a new class B by inheriting it, let B inherit a A, a, a, all the attributes of ' heredity ' A (data attributes and function attributes), implement code reuse
Tip: Use existing classes to create a new class, so that you reuse the existing software part of the set of most of the programming effort, which is often said that the software reuse, not only can reuse their own classes, but also inherit others, such as the standard library, to customize the new data types, This is greatly shorten the software development cycle, for large-scale software development, it is of great significance.
Of course, subclasses can also add their own new properties or redefine them here (without affecting the parent class), and it is important to note that once you have redefined your own properties and have the same name as the parent, you will be able to invoke the new attributes when you call them.
Class Riven (Hero):
camp= ' Noxus '
Def attack (Self,enemy): #在自己这里定义新的attack, no longer uses attack of the parent class, and does not affect the parent class
Print (' from Riven ')
Def fly (self): #在自己这里定义新的
Print ('%s is flying '%self.nickname)
In subclasses, the new function attribute of the same name, when editing functions within the function, it is possible to reuse the same function function of the parent class, it should be called the normal function, that is: class names. Func (), this is the same as calling the normal function, so even if the self parameter to pass the value
Class Riven (Hero):
camp= ' Noxus '
def __init__ (Self,nickname,aggressivity,life_value,skin):
hero.__init__ (Self,nickname,aggressivity,life_value) #调用父类功能
Self.skin=skin #新属性
Def attack (Self,enemy): #在自己这里定义新的attack, no longer uses attack of the parent class, and does not affect the parent class
Hero.attack (Self,enemy) #调用功能
Print (' from Riven ')
Def fly (self): #在自己这里定义新的
Print ('%s is flying '%self.nickname)
R1=riven (' Rui Wen ', 57,200, ' bikini ')
R1.fly ()
Print (R1.skin)
Supplement Super
Python2 in the usage:
- 1. Super(own class, self). function name of parent class
- 2. Super can only be used in modern class
Python3 run directly in parentheses
class people:
def __init__ (self,name,age):
Self.name=name
Self.age=age
def walk (self):
Print ("%s is walking"%self.name)
Class Chinese (people):
country= "China"
def __init__ (self,name,age,language= "Chinese"):
Super (). __init__ (Name,age)
Self.language=language
def walk (self,x):
Super (). Walk ()
Print ("---->")
C=chinese ("Karina", "18")
C.walk ("Asaka")
Summary
python\ Inheritance and derivation