문제 1. 클래스 다이어그램의 Cat 클래스를 파이썬 코드로 생성하시오.
클래스다이어그램
classDiagram
class Cat {
+name : str
+color : str
+size : str
+gender : str
+breed : str
+__init__(name: str, color: str, size: str, gender: str, breed: str)
+meow() void
}
참조코드
class 클래스이름:
# 클래스 속성
def __init__(self, 속성리스트):
# 인스턴스 속성 (인스턴스마다 고유의 값)
self.name = name # 이름
...
def 메소드 이름(self):
# print(f"The cat, {self.name}, meows softly!")
# 객체 생성
my_cat = Cat(name="Cat1", color="orange", size= 'middle', gender='male', breed='Domestic short hair')
# 인스턴스 속성 접근
print(my_cat.name)
print(my_cat.breed)
# 인스턴스 매서드 실행
my_cat.meow()
실행결과
Cat1
Domestic short hair
The cat, Cat1, meows softly!
예상코드
더보기
더보기
더보기
더보기
class Cat:
# 클래스 속성
def __init__(self, name, color, size, gender, breed):
# 인스턴스 속성 (인스턴스마다 고유의 값)
self.name = name # 이름
self.color = color # 색깔
self.size = size # 크기
self.gender = gender # 성별
self.breed = breed # 품종
def meow(self):
print(f"The cat, {self.name}, meows softly!")
# 객체 생성
my_cat = Cat(name="Cat1", color="orange", size= 'middle', gender='male', breed='Domestic short hair')
# 인스턴스 속성 접근
print(my_cat.name)
print(my_cat.breed)
# 인스턴스 매서드 실행
my_cat.meow()
문제 2. 도서관 시스템의 Book 클래스를 파이썬 코드로 생성하시오.
클래스다이어그램
classDiagram
class Book {
+title : str
+author : str
+publisher : str
+genre : str
+__init__(title: str, author: str, publisher: str, genre: str)
+show() void
}
실행결과
=== Book Info ===
Title : Clean Code
Author : Robert C. Martin
Publisher : Prentice Hall
Genre : Software Engineering
예상 코드
더보기
더보기
더보기
더보기
class Book:
def __init__(self, title: str, author: str, publisher: str, genre: str):
self.title = title
self.author = author
self.publisher = publisher
self.genre = genre
def show(self) -> None:
"""도서 정보 출력"""
print("=== Book Info ===")
print(f"Title : {self.title}")
print(f"Author : {self.author}")
print(f"Publisher : {self.publisher}")
print(f"Genre : {self.genre}")
# 인스턴스 1개 생성 + show() 호출 예시
book1 = Book(
title="Clean Code",
author="Robert C. Martin",
publisher="Prentice Hall",
genre="Software Engineering"
)
book1.show()
문제 3. 키오스크 시스템의 Coffee 클래스를 파이썬 코드로 생성하시오.
클래스다이어그램
classDiagram
class Coffee {
-menu_id : int
-name : str
-category : str
-price : int
-is_sold_out : bool
+__init__(menu_id: int, name: str, category: str, price: int, is_sold_out: bool=False)
+set_sold_out(is_sold_out: bool) void
+show() void
}
실행결과
=== Coffee Info ===
Menu ID : 101
Name : 아메리카노
Category : 커피
Price : 2000원
Status : 판매중
예상 코드
더보기
더보기
더보기
더보기
class Coffee:
def __init__(
self,
menu_id: int,
name: str,
category: str,
price: int,
is_sold_out: bool = False
):
self.menu_id = menu_id
self.name = name
self.category = category
self.price = price
self.is_sold_out = is_sold_out
def set_sold_out(self, is_sold_out: bool) -> None:
"""품절 상태 변경"""
self.is_sold_out = is_sold_out
def show(self) -> None:
"""메뉴(커피) 정보 출력"""
status = "품절" if self.is_sold_out else "판매중"
print("=== Coffee Info ===")
print(f"Menu ID : {self.menu_id}")
print(f"Name : {self.name}")
print(f"Category : {self.category}")
print(f"Price : {self.price}원")
print(f"Status : {status}")
# 인스턴스 생성 + 출력 예시
coffee1 = Coffee(menu_id=101, name="아메리카노", category="커피", price=2000)
coffee1.show()
'[2] 객체지향 프로그래밍 설계(파이썬) > 2주차. 객체와 클래스' 카테고리의 다른 글
| [2주차] 2.3 [참고] 파이썬 기본 문법 (0) | 2026.03.12 |
|---|---|
| [2주차] 2.1 [개념] 객체와 클래스 이해하기 (0) | 2026.03.12 |