All gists matching topic encapsulation

timmy / Python 類別與屬性訪問器

0 gustos
0 bifurcaciones
1 archivos
Última actividad 10 months ago
此程式展示 Python 類別的屬性封裝,透過 @property 定義 getter 和 setter 方法,實現安全的屬性訪問與修改,並根據年齡決定遊戲類型。
1 class Person(object):
2
3 def __init__(self, name, age):
4 self._name = name
5 self._age = age
6
7 # 訪問器 - getter方法
8 @property
9 def name(self):
10 return self._name
Siguiente Anterior