/class-inheritance.py Secret
Last active
March 30, 2018 00:56
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Animal: | |
def __init__(self, name): | |
self.name = name | |
def run(self): | |
print('{} is running'.format(self.name)) | |
class Cat(Animal): | |
# ClassName(상위클래스) | |
def __init__(self, name): | |
super(Cat, self).__init__(name) | |
# 상위 클래스의 메소드 호출 | |
# super(현재 클래스, self).method() | |
def run(self): | |
print('Cat {} is running'.format(self.name)) | |
def cry(self): | |
print('Cat {} is crying'.format(self.name)) | |
class Rabbit(Animal): | |
def __init__(self, name): | |
super(Rabbit, self).__init__(name) | |
cat = Cat('catty') | |
rabbit = Rabbit('carrot') | |
cat.run() | |
# Cat catty is running | |
cat.cry() | |
# Cat catty is crying | |
rabbit.run() | |
# carrot is running |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment