📘

【Python】Webで使えるPython 基礎編【10Class 継承】

2023/01/18に公開

【10Class 継承】

YouTube: https://youtu.be/QnnTucwIFt8

https://youtu.be/QnnTucwIFt8

今回はクラスの「継承」について解説します。

main.py
class Animal:
  def __init__(self, animal):
    self.animal = animal

  def print_animal(self):
    print(f'I am {self.animal}')

# tama = Animal('cat')
# tama.print_animal()

# pochi = Animal('dog')
# pochi.print_animal()

# class Cat(Animal):

#   def sleeping(self):
#     print('zzz...')

# tama = Cat('cat')
# tama.print_animal()
# tama.sleeping()

class Dog(Animal):
  def __init__(self, animal, voice):
    super().__init__(animal)
    self.voice = voice

  def print_animal(self):
    print(f'{self.voice}')

  def walking(self):
    print('walking')

pochi = Dog('dog', 'wan')
pochi.print_animal()
# pochi.walking()

Discussion