# 類別-2 Class part 2 ## Private variable 我們想要把變數給「封裝」,不希望外部可以輕易改動變數。 ```python= class profile: __age = 10 a = profile() a.age() # AttributeError: 'profile' object has no attribute 'a' ``` Hint: 不過在Python中沒那麼嚴格,你可以用 dir(a),就會發現你只要寫 `a._profile__age` 就可以改這個變數了。 ### property 加上 `@property` 就可以看到,但不能改。 ```python= class profile: __age = 10 @property def age(self): return self.__age a = profile() a.age # 10 a.age = 100 # AttributeError: can't set attribute ``` ### `__dict__`, `vars()` 用 `__dict__`, `vars()` 可以看到物件的所有東東: ```python= class profile: __age = 10 @property def age(self): return self.__age a = profile() print(a.__dict__) print(vars(a)) ``` ## 繼承 待補 強尼待補