[Python of Hikari-] Chapter 09-03 Class (inheritance)

Return to [Table of Contents Link]

[Python] Chapter 09-03 Inheritance

In the previous section, we mentioned creating an object by instantiation from a blueprint called a class. The class diagram of the Bird type object was as follows. image.png

By the way, paying attention to the method, there are only two states, fly () and print_status (). Birds actually sing, so I'd like to consider creating a method called cry this time.

However, there are many different ways of singing birds, such as "Crow" for crows, "Chun Chun" for sparrows, and Pigeon for sparrows. There are various types such as "Kuruppo". In that case, you will have to create a class for each bird type as follows. image.png

However, if you look at the class diagram above, they all differ only in the ** cry () method part **, and the other parts are the same in all the class diagrams.

Focusing on that, I would like to think about how to program only the difference this time.

Inheritance function

You may want to take advantage of an existing class and create a class with minor changes. ** Inheritance ** can be used at this time.

By using the inheritance function, you can inherit the attributes and methods of an existing class and create a class simply by writing the attributes and methods of the difference only.

It is as follows when expressed in a class diagram. image.png

It can be seen that the Crow class, Pigion class, and Sparrow class inherit from the Bird class. As you can see from this figure, you only need to specify the Crow class, Pigion class, and Sparrow class ** cry () ** separately from the Bird class. That way, you don't have to rewrite the program one by one, just add the differences you need.

Terms often used in inheritance

In the above example, we created Crow class, Pigion class, and Sparrow class based on Bird class. At this time, the original class (Bird class this time) is called ** super class **, and the inherited class (Crow, Pigion, Sparrow class this time) is called ** subclass **.

Next, let's focus on the relationship between superclasses and subclasses. The relationship between Bird and Crow can be said to be "Crow is a Bird." Notated in English, it is ** "Crow is a Bird" **. Such inheritance relationships are sometimes called ** is_a relationships **.

Also, from the perspective of Bird (superclass), it is ** specialized ** to Crow (subclass), and from the perspective of Crow (subclass), it is ** generalized to Bird (superclass) **. It is sometimes expressed as being. image.png

Implement superclasses and subclasses

Let's actually write the program. First, the inherited program is generally described as follows.

class superclass name:
Superclass attributes
Processing by superclass methods

class subclass name(Super class name):
Subclass attributes
Processing by subclass methods

However, if you write all of Crow, Pigion, and Sparrow, it will be difficult to see, so this time I will write a subclass of Crow only. Write the following code in samp09_03_01.py </ font> in chap09 </ font>.

.samp09_03_01.py


#Description of superclass
class Bird:
    def __init__(self, name, color):
        print('-----Call the constructor as soon as you instantiate it.-----')
        self.name = name
        self.color = color

    #Method to fly
    def fly(self, m):
        print('-----Call the fly method.-----')
        print(f'With rustling{self.name}Fly in the sky.')
        print(f'{m}Flying at a height of m.')

    #Method to output the status of instantiated Bird
    def print_status(self):
        print('-----print_Call the status method.-----')
        print(f'{self.color}It is a colored bird.')


#Subclass description
class Crow(Bird):
    def cry(self):
        print('-----Call the cry method in the subclass.-----')
        print('It screams.')


#Instantiate a subclass Crow
kur = Crow('Kurosuke', 'black')

#Call the cry method in the subclass
kur.cry()

#Call the fly method in the superclass
kur.fly(10)

#Print in superclass_Call the status method
kur.print_status()

[Execution result] </ font> ----- Call the constructor as soon as you instantiate it. ----- ----- Call the cry method in the subclass. ----- It screams. ----- Call the fly method. ----- Bassaba and Kurosuke fly in the sky. Flying at a height of 10m. ----- Call the print_status method. ----- It is a black bird.

First, we instantiate a crow from a subclass and assign it to ** kur **. And you can see that the cry method in the subclass ** Crow ** is ringing "Crow".

And note that we can also call the methods in the superclass ** Bird **. This is because the ** Crow ** class inherits from the ** Bird ** class, so you can also call the methods of the ** Bird ** class.

Finally

Inheritance often appears in various libraries, but it is a concept that also appears in other languages such as Java and C ++. The terms that often appear in inheritance are terms that always appear in the object-oriented field in the Fundamental Information Technology Engineer Examination, so let's hold them down.

Return to [Table of Contents Link]

Recommended Posts