This is just a memo of the operation check. Referenced here
python 3.6.9
--Fly variable in Bird class --Swim variable in Fish class --The Penguin class is an inheritance of the Bird and Fish classes.
class Bird:
def __init__(self):
self.fly = 'i can flying'
print(self.__class__.__name__ + 'Create class')
class Fish:
def __init__(self):
self.swim = 'i can swimming'
print(self.__class__.__name__ + 'Create class')
class Penguin(Class1, Class2):
def __init__(self):
print(self.__class__.__name__ + 'Create class')
penguin = Penguin()
penguin.fly
#Execution result
AttributeError: 'Penguin' object has no attribute 'fly'
Fish class variable is overwritten by Bird class's __init__
function
class Bird():
def __init__(self):
self.fly = 'i can flying'
print(self.__class__.__name__)
class Fish():
def __init__(self):
self.swim = 'i can swimming'
print(self.__class__.__name__)
class Penguin(Bird, Fish):
def __init__(self):
super().__init__()
print(self.__class__.__name__)
penguin = Penguin()
penguin.fly
penguin.swim
#Execution result
AttributeError: 'Penguin' object has no attribute 'swim'
It can be inherited without being overwritten by explicitly inheriting the __init__'function with the super () function in the
__init __` function of each class.
class Bird(object):
def __init__(self):
super(Bird, self).__init__()
self.fly = 'i can flying'
print('In the Bird class')
class Fish(object):
def __init__(self):
super(Fish, self).__init__()
self.swim = 'i can swimming'
print('In the Fish class')
class Penguin(Bird, Fish):
def __init__(self):
super(Penguin,self).__init__()
print('In the Fish class')
penguin = Penguin()
print(penguin.fly)
print(penguin.swim)
--The Pokemon class has life and power class variables --Pichu class is inheritance of Pokemon class --Pikachu class is an inheritance of Pichu class --Richu class is inheritance of Pikachu class
--Set initial values for Pichu, Pikachu, Raichu
class Pokemon():
def __init__(self, life=0, power=0):
self.life = life
self.power = power
print('In Pokemon class')
print(self.__class__.__name__ + 'class')
print('life: {} power: {}'.format( self.life, self.power))
print('\n↓\n')
class Pichu(Pokemon):
def __init__(self, life=50, power=100):
super().__init__(life, power)
print('In Pichu class')
print(self.__class__.__name__ + 'class')
print('life: {} power: {}'.format( self.life, self.power))
print('\n↓\n')
def kamituku(self):
print('Biting')
class Pikachu(Pichu):
def __init__(self, life=150, power=200):
super().__init__(life, power)
print('In Pikachu class')
print(self.__class__.__name__ + 'class')
print('life: {} power: {}'.format( self.life, self.power))
print('\n↓\n')
def denkishokku(self):
print('Electric shock')
class Raichu(Pikachu):
def __init__(self, life=200, power=250):
super().__init__(life, power)
print('In Raichu class')
print(self.__class__.__name__ + 'class')
print('life: {} power: {}'.format( self.life, self.power))
print('\n')
def kaminari(self):
print('Thunder')
def set(self, life, power):
self.life = life
self.power = power
print('---------------------------')
print('◯ pokemon instance ◯')
print('---------------------------')
pokemon = Pokemon()
print('---------------------------')
print('◯ pichu instance ◯')
print('---------------------------')
pichu = Pichu()
print('---------------------------')
print('◯ pikachu instance ◯')
print('---------------------------')
pikachu = Pikachu()
print('---------------------------')
print('◯ raichu instance ◯')
print('---------------------------')
raichu = Raichu()
raichu.kamituku()
raichu.denkishokku()
raichu.kaminari()
Execution result
---------------------------
◯ pokemon instance ◯
---------------------------
In Pokemon class
Pokemon class
life: 0 power: 0
↓
---------------------------
◯ pichu instance ◯
---------------------------
In Pokemon class
Pichu class
life: 50 power: 100
↓
In Pichu class
Pichu class
life: 50 power: 100
↓
---------------------------
◯ pikachu instance ◯
---------------------------
In Pokemon class
Pikachu class
life: 150 power: 200
↓
In Pichu class
Pikachu class
life: 150 power: 200
↓
In Pikachu class
Pikachu class
life: 150 power: 200
↓
---------------------------
◯ raichu instance ◯
---------------------------
In Pokemon class
Raichu class
life: 200 power: 250
↓
In Pichu class
Raichu class
life: 200 power: 250
↓
In Pikachu class
Raichu class
life: 200 power: 250
↓
In Raichu class
Raichu class
life: 200 power: 250
Biting
Electric shock
Thunder
--Human has yaruki member variables --Tarou is inheriting Human --The initial value of yaruki of Tarou is 100 --Set tarou's yaruki to 50
--Human has initialization variables --Tarou inheritance of initialization variables
class Human:
def __init__(self, yaruki):
self.yaruki = yaruki
class Tarou(Human):
def __init__(self,yaruki):
super().__init__(yaruki)
def set(self, yaruki):
self.yaruki = yaruki
tarou = Tarou(100)
tarou.set(50)
print(tarou.yaruki)
#Execution result
50
--Neither Human nor Tarou have initialization variables --Tarou has an initial value of 100 for yaruki
class Human:
def __init__(self):
self.yaruki
class Tarou(Human):
def __init__(self):
self.yaruki = 100
def set(self, yaruki):
self.yaruki = yaruki
tarou = Tarou()
tarou.set(50)
tarou.yaruki
#Execution result
50
Recommended Posts