定义一个用户类要处理的信息有用户ID、用户密码、email地址。属性私有化对外提供公开的set和get方法提供一个showInfo方法通过这个方法展示用户信息。编写测试程序创建对象调用showInfo方法。
class User:
def __init__(self, user_id, password, email):
self.__user_id = user_id
self.__password = password
self.__email = email
def get_user_id(self):
return self.__user_id
def set_user_id(self, user_id):
self.__user_id = user_id
def get_password(self):
return self.__password
def set_password(self, password):
self.__password = password
def get_email(self):
return self.__email
def set_email(self, email):
self.__email = email
def show_info(self):
print("User ID:", self.__user_id)
print("Password:", self.__password)
print("Email:", self.__email)
# 测试程序
user = User("123456", "password123", "example@example.com")
user.show_info()
输出结果:
User ID: 123456
Password: password123
Email: example@example.com
``
原文地址: https://www.cveoy.top/t/topic/h6MX 著作权归作者所有。请勿转载和采集!