Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Hello everyone, I am a bowl week, a front end that does not want to be drunk (inrolled). If I am lucky enough to write an article that you like, I am very lucky
Understanding inheritance
Inheritance: A high-level abstraction for code reuse
- Inheritance is one of the essence of object-oriented design
- Implementation of a class – based high-level abstraction level code reuse
- Inheritance is the process by which a newly defined class can use almost all of the attributes and methods of the original class
Whether it’s a base class or a derived class, which is just an inheritance term, these are normal Python classes
It can also be divided into subclasses, superclasses, and superclasses.
The most basic class is the base class, which can be inherited once to produce a derived class, and can be inherited again to produce another derived class. Now the most basic class and the first derived class are parent and subclass, the last derived class is parent and subclass, and the most basic class and last derived class are superclass and subclass
A derived class can inherit not only from a base class, but also from multiple base classes, which is the concept of multiple inheritance
Construction of class inheritance
Class inheritance is declared when it is defined. The syntax is as follows
Class < name > derived class (< class name >) : # base class name can bring path: ModuleNama. BaseClassName def __init__ (self, parameter list > <) : < block >...Copy the code
Derived classes can use the properties and methods of the base class directly
- Base class attributes are basically defined in derived classes
- Derived classes can directly use the class attributes, instance attributes of the base class
- Derived classes can use the various methods of the base class directly
- Class methods and class attributes of the base class are called with the class name of the base class
The sample code
class TestClass: def __init__(self, number): self.sum_number = 0 for i in range(number + 1): self.sum_number += i def sum_num(self): return self.sum_number class HumanNameClass(TestClass): def double_sum(self): Value1 = HumanNameClass(100) print(value1.sum_num()) # 5050 # Use the base class instance method Print (value1.double_sum()) # 10100 # Use of derived class instance methodsCopy the code
Python has two functions that are related to inheritance judgment
function | describe |
---|---|
isinstance(object, classinfo) |
Checks whether the object is an instance of ClassInfo or a subclass, returning True or False |
issubclass(class, classinfo) |
Checks whether the class argument is a subclass of the type argument classinfo, returning True or False. |
The code that follows,
print(isinstance(value1, TestClass)) # True
print(isinstance(value1, HumanNameClass)) # True
print(isinstance(value1, int)) # False
print(issubclass(HumanNameClass, TestClass)) # True
print(issubclass(TestClass, HumanNameClass)) # False
Copy the code
The most basic class in Python
Because everything in Python is an object, any class is an object, and all Python data types are objects. The most basic class that the Python language provides is Object.
- Object is a noun from Python’s most basic class and does not require translation
- All definitions inherit object by default
- Reserved properties and methods are essentially properties and methods of the Object class
The sample code
Print (object.__name__) # Print (object.__bases__) # Print (object.__doc__) # print(object # The most base type print(object.__module__) # Builtins print(Object.__class__) # builtins print(object.__class__) # # <class 'type'>Copy the code
Three elements of a Python object
- logo identity: objects do not change once they are built, using
id()
Get, usually memory address - type type: Specifies the type of the object
type()
To obtain - value value: divided into variablemutableWith the immutableimmutableTwo kinds of
Two built-in Python features related to base classes
Function/keyword | describe |
---|---|
id(x) | Returns the identity of x. In the retainingid()The getMemory () function gets the memory address of an object. |
x is y |
Checks whether the identifiers of x and y are equal, returning True or False, without judging the value |
Overloading of Python classes
Overloading is the redefinition of a base class property or method by a derived class
- Attribute overloading: Derived classes define and use attributes with the same name as the base class
- Method overloading: Derived classes define and use methods with the same name as the base class
Attribute overloading
Attribute overloading adopts the principle of nearby override and no special mark is required for overloading. steps
- Attributes and methods redefined using derived classes are preferred
- Then look for the properties and methods of the base class
- Looking for properties and methods of the superclass
The sample code
Def __init__(self, number): self.sum_number = 0 for I in range(number + 1): self.sum_number += i def sum_num(self): return self.sum_number class HumanNameClass(TestClass): Def __init__(self, number): self.sum_number = 1000 # def double_sum(self): Return self.sum_number * 2 value1 = HumanNameClass(100) print(testClass.text) # Print (value1.text) # Print (value1.text) # Print (value1.text) # print(value1.sum_num()) # 1000Copy the code
Method overloading
Method overloading is the redefinition of a derived class to a base class method; There are full overload and incremental overload
Full overloading: Derived classes completely redefine methods with the same name as the base class
Simply define a method with the same name in a derived class
The sample code
class TestClass: def __init__(self, number): self.sum_number = 0 for i in range(number + 1): self.sum_number += i def sum_num(self): return self.sum_number class HumanNameClass(TestClass): def sum_num(self): Return self.sum_number * 2 value1 = HumanNameClass(100) print(value1.sum_num()) # 10100Copy the code
Incremental overloading: Derived class extensions define methods with the same name and syntax as the base class
Class derived class name < >, < name > base class: def < name > (self, parameter list > <) : super () < name > base class method (< > parameter list)...Copy the code
Incremental overloading uses the super() function
The sample code
Class TestClass1(TestClass): def test_text(self): def test_text(self): def test_text(self): Doc1 = TestClass() doc2 = TestClass1() print(doc1.test_text()) Print (doc2.test_text()) "" -- output result -- this is the base method None # Because the function does not return a value. This is the base method.Copy the code
Multiple inheritance of classes
The construction of multiple inheritance is to declare the inheritance relationship, syntax structure at definition time
Class < class name >(< base name 1>, < base name 2>,... N >, < class name) : # base class name can bring path: ModuleNama. BaseClassName def __init__ (self, parameter list > <) : < block >...Copy the code
Multiple inheritance in Python takes a depth-first, left-to-right approach. Depth-first left-to-right is to start from the left and find its base class. If the base class is not found, then search to the right until the most basic object class is not found.
All attributes and methods are selected depth-first from left to right
Constructors follow the same rules, as does super()
The order of multiple base classes is key
The sample code
Class Test1: def test(self): return text class Test2: def test(self): Return text class Test3(Test1, Test2): pass class Test4(Test2, Test1): pass class Test4(Test2, Test1): Pass value1 = Test3() value2 = Test4() print(value1.test()) print(value2.test()Copy the code
Determine the output sequentially
Understanding the concept of inheritance, the construction of class inheritance, understand that Object is the most basic class in Python
The overloading principle for class attributes is the recently-overridden principle
Overloading of class methods: reloading is similar to overloading of class attributes; Incremental overloading uses the super() function
Multiple inheritance takes a depth-first approach, from left to right