Logo
Section 1 / 5

Python中的封装与私有属性

学习如何以及为何应该隐藏你的数据。

导入

Python中的封装与私有属性

学习如何以及为何应该隐藏你的数据。

...
封装

在 User 类中将密码存储为公共属性

1class User: 2 total_users = 0 3 4 def __init__(self, username, display_name, password): 5 self.username = username 6 self.display_name = display_name 7 self.password = password # 将密码作为公共属性存储 8 self.posts = [ ] 9 User.total_users += 1 10 11 def show_profile(self): 12 print("用户:", self.display_name, "(@" + self.username + ")")

No configuration found for ID: 9cff4c2dbc8c59a08190b979c87950cd769ccf9777794c18c3fbd5111947ce9e

代码示例

1self._password = password
1class User: 2 total_users = 0 3 4 def __init__(self, username, display_name, password): 5 self.username = username 6 self.display_name = display_name 7 self._password = password # 正确:使用前置下划线表示私有性 8 self.posts = [ ] 9 User.total_users += 1 10 11 def show_profile(self): 12 print("用户:", self.display_name, "(@" + self.username + ")") 13 14 def change_password(self, new_password): 15 # 仅当新密码长度不少于6位时才允许修改 16 if len(new_password) >= 6: 17 self._password = new_password 18 print("密码更新成功!") 19 else: 20 print("错误:密码必须至少6个字符长。") 21 22 def check_password(self, input_password): 23 return input_password == self._password

No configuration found for ID: 2a7dcfc18b0341c2d8adf8bb9de8879c3e199cf674b449a6bf7486f7e2761ebc

1class Post: 2 total_posts = 0 3 4 def __init__(self, content, author): 5 self.content = content 6 self.author = author 7 self.likes = [ ] # 公共列表,记录点赞的用户 8 Post.total_posts += 1 9 10 def like_post(self, user): 11 if user not in self.likes: 12 self.likes.append(user) 13 print(user.display_name, "点赞了这条动态!") 14 else: 15 print(user.display_name, "已经点赞过这条动态!") 16 17 @staticmethod 18 def validate_content_length(content): 19 if len(content) <= 280: 20 return True 21 else: 22 return False

代码示例

1Alex 点赞了这条动态! 2私有点赞数(通过获取器):1
  • 使用前置下划线(_like_count)表示该属性为私有。

  • 在 like_post() 方法中添加新点赞时,增加 _like_count。

  • 使用获取器方法(get_like_count())安全地访问私有属性。

  • 使用前置下划线(_like_count)表示属性为私有。

  • 在 like_post() 方法中添加新点赞时,增加 _like_count。

  • 使用获取器方法(get_like_count())安全地访问私有属性。

No configuration found for ID: 19f757a4a09743453515897ff2311b30927b17d45870f75442e08bfd9c73dbf7

1class Post: 2 total_posts = 0 3 4 def __init__(self, content, author): 5 self.content = content 6 self.author = author 7 self.likes = [ ] # 公共列表,记录点赞的用户 8 self._like_count = 0 # 私有属性,用于记录点赞数量 9 Post.total_posts += 1 10 11 def like_post(self, user): 12 if user not in self.likes: 13 self.likes.append(user) 14 self._like_count += 1 # 增加私有点赞计数 15 print(user.display_name, "点赞了这条动态!") 16 else: 17 print(user.display_name, "已经点赞过这条动态!") 18 19 def get_like_count(self): 20 return self._like_count # 安全地获取点赞数量 21 22 @staticmethod 23 def validate_content_length(content): 24 if len(content) <= 280: 25 return True 26 else: 27 return False

解决方案说明

  1. 私有属性(_like_count):

    • 添加用于安全追踪点赞数量(第8行)。

    • 无法从类外部直接访问。

  2. 受控更新:

    • _like_count 仅在 like_post() 方法内部更新(第14行)。
  3. 公共获取器(get_like_count()):

    • 提供安全方式访问点赞数量,而无需暴露私有属性(第19–20行)。
1class Post: 2 total_posts = 0 3 4 def __init__(self, content, author): 5 self.content = content 6 self.author = author 7 self.likes = [ ] # 公共列表,记录点赞的用户 8 self._like_count = 0 # 私有属性,用于记录点赞数量 9 Post.total_posts += 1 10 11 def like_post(self, user): 12 if user not in self.likes: 13 self.likes.append(user) 14 self._like_count += 1 # 增加私有点赞计数 15 print(user.display_name, "点赞了这条动态!") 16 else: 17 print(user.display_name, "已经点赞过这条动态!") 18 19 def get_like_count(self): 20 return self._like_count # 安全地获取点赞数量 21 22 @staticmethod 23 def validate_content_length(content): 24 if len(content) <= 280: 25 return True 26 else: 27 return False

解决方案说明

  1. 私有属性(_like_count):

    • 添加用于安全追踪点赞数量(第8行)。

    • 无法从类外部直接访问。

  2. 受控更新:

    • _like_count 仅在 like_post() 方法内部更新(第14行)。
  3. 公共获取器(get_like_count()):

    • 提供安全方式访问点赞数量,而无需暴露私有属性(第19–20行)。

核心类与对象

介绍 SocialNetwork(组合)