Tạo một lớp chắt (grandchild)
Trong bài tập này, bạn sẽ dùng tính kế thừa để tạo lớp Tweets từ lớp SocialMedia. Lớp chắt mới này Tweets sẽ xử lý các chi tiết đặc thù của Twitter như retweet.
Bài tập này là một phần của khóa học
Nguyên tắc Kỹ thuật Phần mềm với Python
Hướng dẫn bài tập
- Hoàn thiện câu lệnh
classđểTweetskế thừa từSocialMedia.SocialMediađã được nạp sẵn trong môi trường của bạn. - Dùng
super()để gọi phương thức__init__của lớp cha. - Định nghĩa
retweet_text. Dùnghelp()để hoàn thiện lời gọi tớifilter_linesvới tên tham số chính xác.filter_linesđã được nạp sẵn trong môi trường của bạn. returnretweet_texttừ_process_retweetsnhư một thể hiện củaSocialMedia.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
# Define a Tweet class that inherits from SocialMedia
class Tweets(____):
def __init__(self, text):
# Call parent's __init__ with super()
____
# Define retweets attribute with non-public method
self.retweets = self._process_retweets()
def _process_retweets(self):
# Filter tweet text to only include retweets
retweet_text = filter_lines(self.text, ____='RT')
# Return retweet_text as a SocialMedia object
return ____(retweet_text)