pass (일단 넘어가기, 함수에서 break 같은 것)
- 건물 유닛으로 예시1
#일반 유닛
class unit:
def __init__(self,name,hp,speed):
self.name=name
self.hp=hp
self.speed=speed
def move(self,location):
print("[지상 유닛 이동]")
print("{0}:{1} 방향으로 이동합니다. [속도{2}]"\
.format(self.name,location,self.speed))
#건물 유닛
class buildingunit(unit):
def __init__(self,name,hp,location):
pass #일단은 완성된 것 처럼 보여줄 수 있음.
#서플라이 디폿: 건물, 1개 건물= 8유닛
supply_depot = buildingunit("서플라이 디폿",500,"7시")
밑에서 두번 째 문단에서 buildingunit 클래스를 작성할 때 그냥 pass 적음으로써 완성된 것 처럼 보이게 함.(오류 안뜸)
그래서 supply_depot를 buildingunit 클래스에서 가져오려고 값을 입력해도 아무 것도 작성이 안됨.
-게임 스타트와 게임 오버로 예시2
def game_start():
print("[알림]새로운 게임을 시작합니다.")
def game_over():
pass
game_start()
game_over()
Terminal>>
[알림]새로운 게임을 시작합니다.
위와 같이 game_over()에 대한 값은 나오지 않음. pass를 썼기 때문에!
super (상속받은 유닛의 내용을 초기화 할 때)
상속받은 유닛에 있는 것을 초기화하고 다시 메소드를 짜고 싶을 때
-원래처럼 unit.__init__(self, 요소1,요소...)를 활용해서 초기화 하기
class buildingunit(unit):
def __init__(self,name,hp,location):
unit.__init__(self,name,hp,0) #speed 자리에는 0, 움직일 수 없는 유닛임
self.location=location
-super를 이용해서 초기화하기
class buildingunit(unit):
def __init__(self,name,hp,location):
super().__init__(name,hp,0) #super 사용 시 ()붙이고 init 다음 self는 삭제 꼭!
self.location=location
1. 이때 주의점은 super() 가 세트이고 .__init__다음 self를 삭제 해야한다는 것!
-super를 이용해서 초기화하기2 (다중상속 받을 때의 문제점)
class unit:
def __init__(self):
print("unit 생성자")
class flyable:
def __init__(self):
print("flyable 생성자")
class flyableunit(flyable,unit):
def __init__(self):
super().__init__() #super쓸 때 self 없애야함
#드랍쉽
dropship = flyableunit ()
Terminal>>
flyable 생성자
*다중상속인 경우 super를 쓰면 (첫 번째 유닛, 두 번째 유닛) 중에서 두 번째 유닛은 적용 안됨.
즉, 다중상속인 경우 super가 아닌
첫 번째 유닛.__init__(self)
두 번째 유닛.__init__(self)를 사용할 것!
-__init__을 활용한 다중상속 초기화
class flyableunit(flyable,unit):
def __init__(self):
# super().__init__() #super쓸 때 self 없애야함
unit.__init__(self)
flyable.__init__(self)
#드랍쉽
dropship = flyableunit ()
Terminal>>
unit 생성자
flyable 생성자
class 관련 강사님 퀴즈
내가 만든 답변
class house:
def __init__(self,location,house_type,deal_type,price,completion_year):
self.location=location #매개변수로 만들기
self.house_type=house_type
self.deal_type=deal_type
self.price=price
self.completion_year=completion_year
def show_detail(self):
print(self.location,self.house_type,self.deal_type,\
self.price,self.completion_year)
#매물 정보
house1=house("강남","아파트","매매","10억","2010년")
house2=house("마포","오피스텔","매매","5억","2007년")
house3=house("송파","빌라","매매",500/50,"2000년")
ㅎ.. 역시나 안됨.
강사님 답변
class house:
def __init__(self,location,house_type,deal_type,price,completion_year):
self.location=location #매개변수로 만들기
self.house_type=house_type
self.deal_type=deal_type
self.price=price
self.completion_year=completion_year
def show_detail(self):
print(self.location,self.house_type,self.deal_type,\
self.price,self.completion_year)
houses=[]
house1=house("강남","아파트","매매","10억","2010년")
house2=house("마포","오피스텔","매매","5억","2007년")
house3=house("송파","빌라","매매","500/50","2000년")
#show_detail 출력할 때 반복문 이용하려고 리스트 활용
houses.append(house1) #house라는 리스트에 house1 추가
houses.append(house2)
houses.append(house3)
print("총{0}대의 매물이 있습니다.".format(len(houses)))
for house in houses:
house.show_detail()
1. houses=[] 라고 리스트를 만들어버림.
2. house1, house2,house3 까지 만드는건 잘 했음
3. house1,2,3 애들을 houses 리스트 안에 넣어야하므로 houses.append(house1,2,3)
4. "총 {0}대의 매물이 있습니다" 에서 houses 리스트의 개수를 len으로 처리!!! ★ len으로도 요소개수를 셀 수 있구나!
5. 리스트에서 하나씩 house 꺼 내 와 서 => for house in houses:
6. house에 show_detail이라는 함수 적용해서 출력하기 => house.show_detail()
'Python' 카테고리의 다른 글
[파이썬] 모듈 ( import, from import , package, detail함수, __all__,__name__) (2) | 2024.11.10 |
---|---|
[파이썬] 예외처리(try, except, ValueError, ZeroDivision, raise, __str__, break, finally) (0) | 2024.11.10 |
[파이썬]클래스class Ⅱ (상속, 다중상속, 메소드오버라이딩) (0) | 2024.11.09 |
[파이썬] 클래스class (__init__,멤버변수, 메소드) (1) | 2024.11.09 |
[파이썬] 파일 입출력(write, read, pickle, with) (4) | 2024.11.09 |