Sep-28-2021, 02:59 AM
I'm new in Programing Language.
in here Bird and Cat are inheritance from Animal,
- Bird use __init__ and super
- Cat use nothing
but both have good result, inheritance from animal, the function work perfectly
is there explanation why we still use __init__ super since without it, inheritance still working and less code
Thanks
in here Bird and Cat are inheritance from Animal,
- Bird use __init__ and super
- Cat use nothing
but both have good result, inheritance from animal, the function work perfectly
is there explanation why we still use __init__ super since without it, inheritance still working and less code
Thanks
class Animal(): varA = "---" def __init__(self,name,age): self.name = name self.age = age print(self.name + " has been created") def talk(self): print(self.name + "Say : ...") def myinfo(self): print(self.varA + " Info name: " + self.name + " | Info age: " + str(self.age) + " " + self.varA) class Cat(Animal): def talk(self): print(self.name + " Say: Meow") class Bird(Animal): def __init__(self,name,age): super().__init__(name,age) def talk(self): print(self.name + " Say: BrrrBrr") print("------------------------------") randomAnimal = Animal("randomAnimal",9) randomAnimal.talk() randomAnimal.myinfo() print("------------------------------") blackCat = Cat("BlackCat",2) blackCat.talk() blackCat.myinfo() print("------------------------------") blueBird = Bird("BlueBird",3) blueBird.talk() blueBird.myinfo() print("------------------------------")Result Code:
Quote:------------------------------
randomAnimal has been created
randomAnimalSay : ...
--- Info name: randomAnimal | Info age: 9 ---
------------------------------
BlackCat has been created
BlackCat Say: Meow
--- Info name: BlackCat | Info age: 2 ---
------------------------------
blueBird has been created
blueBird Say: BrrrBrr
--- Info name: BlueBird | Info age: 3 ---
------------------------------