매서드 오버라이딩 구현 코드
class Car:
color = ""
speed= 0
#차의 기능
def upSpeed(self,value): #self에는 호출한 인스턴스의 정보가 들어감
self.speed += value
def downSpeed(self, value):
self.speed -= value
class Sedan(Car):
seat = 0
def getSeat(self):
return slef.seat
def upSpeed(self,value): #재정의(매서드 오버라이딩)
self.speed += value+30
mySedan = Sedan()
mySedan.color = "빨강"
mySedan.speed = 10
mySedan.seat = 5
mySedan.upSpeed(50)
print("mySedan: %s, %d, %d" %(mySedan.color, mySedan.speed, mySedan.seat))
매서드 오버라이딩을 활용해 차의 기능인 upSpeed() 함수를 재정의 했다.
실행 결과.
매서드 오버라이딩을 통해 speed 값에 +30을 해주는 새로운 upSpeed를 구현하였으므로
Sedan의 speed 값은 90으로 출력된다.
'GURU > PYTHON & HTML' 카테고리의 다른 글
[PYTHON Unit 11: 파이썬 실전] 2차원 list 다루기 (0) | 2022.01.11 |
---|---|
[PYTHON Unit 11: 파이썬 실전] list 다루기 (0) | 2022.01.11 |
[PYTHON Unit 10: 클래스] 매개변수가 있는 생성자 (0) | 2022.01.10 |
[PYTHON Unit 10: 클래스] 자동차 클래스 (0) | 2022.01.10 |
[PYTHON Unit 9: 함수 심화] starbucks (0) | 2022.01.10 |