開発環境
- macOS Catalina - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.8 (プログラミング言語)
Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 14(Object-Oriented Programming)、Exercise 3の解答を求めてみる。
コード
#!/usr/bin/env python3
class Member:
def __init__(self, name: str, address: str, email: str) -> None:
self.name = name
self.address = address
self.email = email
def __str__(self):
return f'{self.name}\n{self.address}\n{self.email}'
def __repr__(self):
s = str(self).replace('\n', ', ')
return f'{self.__class__.__name__}({s})'
class Faculty(Member):
def __init__(self, name: str, address: str, email: str,
faculty_num: str) -> None:
super().__init__(name, address, email)
self.faculty_num = faculty_num
def __str__(self):
return f'{super().__str__()}\n{self.faculty_num}'
def __repr__(self):
s = str(self).replace('\n', ', ')
return f'{self.__class__.__name__}({s})'
class Student(Member):
def __init__(self, name: str, address: str, email: str,
student_num: str) -> None:
super().__init__(name, address, email)
self.student_num = student_num
self.courses_taken = []
self.courses_taking = []
def __str__(self):
return f'{super().__str__()}\n' + \
f'taken: {self.courses_taken}\ntaking: {self.courses_taking}'
def __repr__(self):
s = str(self).replace('\n', ', ')
return f'{self.__class__.__name__}({s})'
if __name__ == "__main__":
student = Student("name1", "address1", "email1", "1")
member = Member('name2', 'address2', 'email2')
faculty = Faculty('name3', 'address3', 'email3', '3')
members = [student, member, faculty]
for member in members:
print(type(member), member, repr(member), sep='\n')
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./university.py
<class '__main__.Student'>
name1
address1
email1
taken: []
taking: []
Student(name1, address1, email1, taken: [], taking: [])
<class '__main__.Member'>
name2
address2
email2
Member(name2, address2, email2)
<class '__main__.Faculty'>
name3
address3
email3
3
Faculty(name3, address3, email3, 3)
%
0 コメント:
コメントを投稿