본문 바로가기
Programing/Python

5-1 파이썬 클래스 02

by BroJune 2021. 8. 17.

- ClassTest03

 

# "" 클래스를 확장한(상속한) "비행기" 클래스를 만들고
# fly 메소드를 오버라이딩 해 봅니다.

class Bird:
def __init__(self,color,type,wing):
self.color = color
self.type = type
self.wing = wing

def fly(self):
if self.wing:
print(self.color,self.type,"() ~ ")

else:
print(self.color,self.type,"() 날개가 없어서 날 수 없어요!")


class Plane(Bird):
def fly(self):
if self.wing:
print(self.color, self.type, "~~ ~ 하고 날라 갔습니다!")

else:
print(self.color,self.type,"() 수리 중이라 날 수 없어요!")


a = Plane("하늘색", "보잉 747", True)
a.fly()
c = Plane("하늘색", "보잉 747", False)
c.fly()

b = Bird("노랑색", "참새", True)
b.fly()
d = Bird("노랑색", "참새", False)
d.fly()

 

- ClassTest04

 

# "" 클래스를 확장한(상속한) "비행기" 클래스를 만들고
# fly 메소드를 오버라이딩 해 봅니다.
# , "비행기" 클래스에는 속성 "엔진"을 추가하도록 하고
# 날개도 있고 엔진도 있어야 날 수 있어야 합니다.


class Bird:
def __init__(self,type, color, wing):
self.type = type
self.color = color
self.wing = wing

def fly(self):
if self.wing:
print(self.color,self.type,"() ~ ")

else:
print(self.color,self.type,"() 날개가 없어서 날 수 없어요!")


class Plane(Bird):
def __init__(self,type,color,wing,engine):
self.type = type
self.color = color
self.wing = wing
self.engine = engine

def fly(self):
if self.wing and self.engine:
print(self.type, self.color, "() ~~ ~ 하고 날라 갔습니다!")

else:
print(self.type, self.color, "() 수리 중이라 날 수 없어요!")


a = Plane("보잉 747", "하늘색", True, True)
a.fly()
c = Plane("보잉 747", "하늘색", False, False)
c.fly()
e = Plane("보잉 747", "하늘색", True, False)
e.fly()
f = Plane("보잉 747", "하늘색", False, True)
f.fly()


b = Bird("노랑색", "참새", True)
b.fly()
d = Bird("노랑색", "참새", False)
d.fly()

- ClassTest05

 

# "" 클래스를 확장한(상속한) "비행기" 클래스를 만들고
# fly 메소드를 오버라이딩 해 봅니다.
# , "비행기" 클래스에는 속성 "엔진"을 추가하도록 하고
# 날개도 있고 엔진도 있어야 날 수 있어야 합니다.
# 자식 클래스인 "비행기" 에서는
# type,color,wing 의 초기화는
# 부모클래스인 "" 클래스에게 맡겨요

class Bird:
def __init__(self,type, color, wing):
self.type = type
self.color = color
self.wing = wing

def fly(self):
if self.wing:
print(self.color,self.type,"() ~ ")

else:
print(self.color,self.type,"() 날개가 없어서 날 수 없어요!")


class Plane(Bird):
def __init__(self,type,color,wing,engine):
super().__init__(type,color,wing) # 부모의 생성자를 호출
self.engine = engine

def fly(self):
if self.wing and self.engine:
print(self.type, self.color, "() ~~ ~ 하고 날라 갔습니다!")

else:
print(self.type, self.color, "() 수리 중이라 날 수 없어요!")


a = Plane("보잉 747", "하늘색", True, True)
a.fly()
c = Plane("보잉 747", "하늘색", False, False)
c.fly()
e = Plane("보잉 747", "하늘색", True, False)
e.fly()
f = Plane("보잉 747", "하늘색", False, True)
f.fly()

b = Bird("노랑색", "참새", True)
b.fly()
d = Bird("노랑색", "참새", False)
d.fly()

 

- 참고 자료 : Do it! 점프 투 파이썬 https://wikidocs.net/12 -

 

위키독스

온라인 책을 제작 공유하는 플랫폼 서비스

wikidocs.net

 

 

'Programing > Python' 카테고리의 다른 글

5-1 파이썬 클래스 04  (0) 2021.08.17
5-1 파이썬 클래스 03  (0) 2021.08.17
5-1 파이썬 클래스 01  (0) 2021.08.17
4-3 파이썬 파일 읽고 쓰기  (0) 2021.08.17
4-2 파이썬 input  (0) 2021.08.17