-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass8_2.py
More file actions
45 lines (33 loc) · 797 Bytes
/
class8_2.py
File metadata and controls
45 lines (33 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# classのプロパティ
# 新規作成 2023/3/16
class Post:
def __init__(self, text):
self._text = text
self._likes = 0
def show(self):
print(f"{self._text} - {self._likes}")
def like(self):
self._likes += 1
# # setter
# def set_likes(self, num):
# self._likes = num
# # getter
# def get_likes(self):
# return self._likes
@property
def likes(self):
return self._likes
# コメントアウトして読み取り専用にする
# @likes.setter
# def likes(self,num):
# self._likes = num
posts = [
Post("Hello"),
Post("Hi"),
]
# posts[0]._likes = 100
# print(posts[0]._likes)
# posts[0].likes = 100
print(posts[0].likes) # 100
# for post in posts:
# post.show()