강의

멘토링

로드맵

Inflearn Community Q&A

gulsamcono1891's profile image
gulsamcono1891

asked

Free Python Tutorial (Basic) - Become a Developer in 6 Hours

Method Overriding

질문이 있습니다.

Written on

·

208

1

1. Flyable 클래스에 이름정보가 따로 저장이 안 되었다는 말이 무슨 의미인가요? Flyalbe의 __init__ 메소드에 name 파라미터를 넣어준다면 fly 메소드의 name 파라미터는 안 넣어도 됐을 거라는 뜻인가요?

class Flyable:
    def __init__(selfnameflying_speed):
        self.flying_speed = flying_speed

    def fly(selflocation):
        print (f"{self.name} : {location} 방향으로 날아갑니다. [속도 {self.flying_speed}]")

class FlyableAttackUnit(AttackUnitFlyable):
    def __init__(selfnamehpdamageflying_speed):
        AttackUnit.__init__(self, name, hp, 0, damage) # 지상 speed = 0
        Flyable.__init__(self, name, flying_speed)

    def move(selflocation):
        print ("[공중유닛 이동]")
        self.fly(location)
battlecruiser = FlyableAttackUnit("배틀크루저"500253)
battlecruiser1 = FlyableAttackUnit("배틀크루저"500253)

battlecruiser1.fly ("1시")
battlecruiser.move("9시")

한 번 수정해봤는데 큰 문제 없을까요? 이러면 배틀크루저.name은 AttackUnit과 Flyable 양쪽 2개가 생성되는 건가요?

2. 메소드 오버라이딩은 상속받은 클래스에 존재하는 같은 이름의 메소드를 대체하게 되는 건가요? 그렇다면 기존의 Unit의 move 메소드는 덮어씌우져서 쓸 수 없게 되는건가요?

python

Quiz

41% of people got it wrong. Give it a try!

Why is it more efficient to use a class rather than separate variables for each when representing multiple similar objects?

As variable naming rules became simpler,

Reducing code duplication and making management easier

Program running speed is much faster

As memory usage has greatly decreased

Answer 1

1

1. 아마 메모리 주소를 사용하는 것이라, 생성보다는 가르킨다는 표현이 맞을껍니다. 하지만 그런 식으로 관리하면 나중에 객체 관리가 힘들어 집니다.

2.네 아래는 간단한 예제입니다.

class ParentA:
    def __init__(self , name):
        self.name = name
    
    def whois(self):
        print("my name is {0} ".format(self.name))

class ChildA(ParentA):
    def __init__(self , name):
        self.name = name
    
    def whois(self):
        print("my parent name is {0} ".format(self.name))

parentA  = ParentA("A")
parentA.whois() # my name is A

childA = ChildA("A")
childA.whois() # my parent name is A

gulsamcono1891's profile image
gulsamcono1891

asked

Ask a question