nanenj icon

Python OOP example

nanenj | PRO | 12/02/19 11:39:48 PM UTC | 0 ⭐ | 310 👁️ | Never ⏰ | []
Python |

400 B

|

None

|

0 👍

/

0 👎

class Animal:
    def __init__(self, name):
        self.name = name
 
    def speak(self):
        print("This animal is named: " + self.name)
        print("It doesn't make a specific sound.")
 
a1 = Animal("fred")
a1.speak()
 
class Dog(Animal):
    def __init__(self, name):
        super().__init__(name)
 
    def speak(self):
        print("This animal is named: " + self.name)
        print("It goes Arf!")
 
d1 = Dog("Fido")
d1.speak()

Comments