출처 : 반크_반크 20년 백서
참고풀이]
#연락처 클래스를 만든다.
class Person():
#인스턴스 변수 name, mobile, office, email를 가지는 생성자 함수
def __init__(self, name, mobile=None, office=None, email=None):
self.name = name
self.mobile = mobile
self.office = office
self.email = email
#Person의 정보를 문자열로 반환하는 함수
def __str__(self):
Str = ""
Str += self.name + "\n"
if self.mobile != None:
Str += "mobile phone : " + self.mobile + "\n"
if self.office != None:
Str += "office phone : " + self.office + "\n"
if self.email != None:
Str += "email address: " + self.email + "\n"
return Str
#각 속성의 설정자 함수들
def setName(self, name):
self.name=name
def setMobile(self, mobile):
self.mobile=mobile
def setOffice(self,office):
self.office=office
def setEmail(self, email):
self.email=email
#각 속성의 접근자 함수들
def getName(self):
return self.name
def getMobile(self):
return self.mobile
def getOffice(self):
return self.office
def getEmail(self):
return self.email
#연락처를 저장하는 클래스를 만든다.
class PhoneBook():
#생성자 함수에서 딕셔너리를 생성한다.
def __init__(self):
self.contacts = {}
#연락처를 문자열로 변환하는 함수
def __str__(self):
Str = ""
for p in sorted(self.contacts):
Str += str(self.contacts[p]) + '\n'
return Str
#한 사람의 연락처를 추가하는 함수
def add(self, name, mobile=None, office=None, email=None):
p = Person(name, mobile, office, email)
self.contacts[name] = p
#Main 부분
obj = PhoneBook()
obj.add("Kim", office="1234567", email="kim@company.com")
obj.add("Park", office="2345678", email="park@company.com")
#결과출력
print(obj)
참고풀이 결과]
대한민국의 아름다운 영토, 독도의 여름
'프로그램 > Python 1000제' 카테고리의 다른 글
Python 198제] 파이썬 EXPRESS CHAPTER 8. Programming 9 p403 (0) | 2023.06.12 |
---|---|
Python 197제] 파이썬 EXPRESS CHAPTER 8. Programming 8 p403 (0) | 2023.06.12 |
Python 195제] 파이썬 EXPRESS CHAPTER 8. Programming 6 p402 (0) | 2023.06.12 |
Python 194제] 파이썬 EXPRESS CHAPTER 8. Programming 5 p402 (0) | 2023.05.22 |
Python 193제] 파이썬 EXPRESS CHAPTER 8. Programming 4 p401 (0) | 2023.05.22 |
댓글