# 중국집 주문 접수
class order :
# self를 제외한 괄호 안의 값을 전달하면 그 값이 가공을 거쳐 반환된다.
def __init__(self, address, menu, amount, price) :
self.address = address
self.menu = menu
self.amount = amount
self.price = price
print("{0:>20}".format("주문이 접수되었습니다."))
print("{0:>20}".format("- 주문내역 -"))
print("주소 : {0}".format(address))
print(f"메뉴 : {menu}\t수량 : {amount}\t총액 : {amount*price}\n")
# order 클래스에 값 전달
order_1 = order("파이썬빌라 201호", "짜장면", 3, 5000)
order_2 = order("파이썬아파트 102동 403호", "짬뽕", 2, 6000)
# 반환
'''
주문이 접수되었습니다.
- 주문내역 -
주소 : 파이썬빌라 201호
메뉴 : 짜장면 수량 : 3 총액 : 15000
주문이 접수되었습니다.
- 주문내역 -
주소 : 파이썬아파트 102동 403호
메뉴 : 짬뽕 수량 : 2 총액 : 12000
'''
# 이전에 생성한 중국집 주문접수
class order :
def __init__(self, address, menu, amount, price) :
self.address = address
self.menu = menu
self.amount = amount
self.price = price
print("{0:>20}".format("주문이 접수되었습니다."))
print("{0:>20}".format("- 주문내역 -"))
print("주소 : {0}".format(address))
print(f"메뉴 : {menu}\t수량 : {amount}\t총액 : {amount*price}\n")
order_1 = order("파이썬빌라 201호", "짜장면", 3, 5000)
order_2 = order("파이썬아파트 102동 403호", "짬뽕", 2, 6000)
# 각 객체의 멤버변수는 아래와 같이 외부에서 호출이 가능하다.
print("{0}에 {1} 배달이 완료되었습니다.".format(order_1.address, order_2.menu))
# 클래스를 통해 생성한 객체에 외부에서 변수를 추가할 수 있다.
order_1.use_coupon = True
# 이전에 생성한 중국집 주문접수
class order :
def __init__(self, address, menu, amount, price) :
self.address = address
self.menu = menu
self.amount = amount
self.price = price
print("{0:>20}".format("주문이 접수되었습니다."))
print("{0:>20}".format("- 주문내역 -"))
print("주소 : {0}".format(address))
print(f"메뉴 : {menu}\t수량 : {amount}\t총액 : {amount*price}\n")
# 클래스 내부에 메소드 추가하기
# 클래스와 메소드에서 self는 호출될 객체 자기자신을 의미한다.
def cancel(self, reason):
print("{0} 주문을 취소하였습니다. 사유 : {1}"\
.format(self.address, reason))
# 객체 생성
order_1 = order("파이썬빌라 201호", "짜장면", 3, 5000)
order_2 = order("파이썬아파트 102동 403호", "짬뽕", 2, 6000)
# order_1에 메소드 적용
# order_1의 정보를 불러온 뒤 cancel을 적용시킨다.
order_1.cancel("배달 불가 지역")
# => 파이썬빌라 201호 주문을 취소하였습니다. 사유 : 배달 불가 지역
# 일반 주문과 배달 주문을 구분하여 받기로 하였다
# 일반 주문
class order:
def __init__(self, menu, amount, price) :
self.menu = menu
self.amount = amount
self.price = price
print("{0:>20}".format("주문이 접수되었습니다."))
print("{0:>20}".format("- 주문내역 -"))
print(f"메뉴 : {menu}\t수량 : {amount}\t총액 : {amount*price}\n")
def cancel(self, reason):
print("주문을 취소하였습니다. 사유 : {0}"\
.format(reason))
# 배달 주문
class order_delivery(order): # order 클래스에서 상속을 받는다.
# 다중 상속일 경우 콤마로 구분
def __init__(self, address, menu, amount, price):
# order에서 상속 받을 변수를 호출한다.
# 다중 상속일 경우 클래스별로 따로 호출한다.
order.__init__(self, menu, amount, price)
# 현재 클래스에서만 사용할 변수를 추가한다.
self.address = address
print("주소 : {0}".format(address))
def delivery_cancel(self, reason):
print("{0} 배달 주문을 취소하였습니다. 사유 : {1}"\
.format(self.address, reason))
order_1 = order("짜장면", 3, 5000)
order_2 = order_delivery("파이썬아파트 102동 403호", "짬뽕", 2, 6000)
class order:
def __init__(self, menu, amount, price) :
self.menu = menu
## self.amount = amount
self.price = price
print("{0:>20}".format("주문이 접수되었습니다."))
print("{0:>20}".format("- 주문내역 -"))
print(f"메뉴 : {menu}\t수량 : {amount}\t\
총액 : {amount*price}\n")
# 부모 클래스 함수
# 부모 클래스 객체는 cancel을 호출하면 이 함수가 적용된다.
def cancel(self, reason):
print("주문을 취소하였습니다. 사유 : {0}"\
.format(reason))
class order_delivery(order):
def __init__(self, address, menu, amount, price):
order.__init__(self, menu, amount, price)
self.address = address
print("주소 : {0}".format(address))
# 자식 클래스 함수
# 함수 이름은 부모와 같지만 내용은 다르다.
# 자식 클래스 객체는 cancel을 호출하면 이 함수가 적용된다.
def cancel(self, reason):
print("{0} 배달 주문을 취소하였습니다. 사유 : {1}"\
.format(self.address, reason))
def hello():
print("안녕하세요")
# "안녕하세요"가 출력된다.
def abc():
pass
# 아무 작업도 수행하지 않으며, 오류 등의 말썽 없이 다음 작업으로 넘어간다.
def bye():
print("안녕히 가세요")
# "안녕히 가세요"가 출력된다.
class order_delivery(order):
def __init__(self, address, menu, amount, price):
# super를 이용한 상속
# 부모 클래스명과 self가 생략된다.
super().__init__(menu, amount, price)
self.address = address
print("주소 : {0}".format(address))
총 3대의 매물이 있습니다.
강남 아파트 매매 10억 2010년
마포 오피스텔 전세 5억 2007년
송파 빌라 월세 500/50 2000년
class House:
house_list = [] # 매물 리스트를 클래스 변수로 생성
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.competion_year = completion_year
House.house_list.append(self) # 인스턴스를 매물 리스트에 추가
def show_detail(self): # 매물 정보를 출력할 함수 생성
print(self.location, self.house_type, self.deal_type, self.price, self.competion_year)
# House 클래스로 매물 정보 입력
gangnam = House("강남", "아파트", "매매", "10억", "2010년")
mapo = House("마포", "오피스텔", "전세", "5억", "2007년")
songpa = House("송파", "빌라", "월세", "500/50", "2000년")
# 내용 출력
print("총 {0}대의 매물이 있습니다.".format(len(House.house_list)))
for i in House.house_list:
i.show_detail()