类和对象
main.py:
from user import User from post import Post app_user_one = User("nn@nn.com", "Nana Janshi", "pwd1", "DevOps Engineer") app_user_one.get_user_info() app_user_one.change_job_title('xxx') app_user_one.get_user_info() app_user_two = User("aaa@aa.com", "James Bond", "saasd", "xxx") app_user_two.get_user_info() new_post = Post("on a secret mission today", app_user_two.name) new_post.get_post_info()
user.py:
class User: def __init__(self, user_email, name, password, current_job_title): self.email = user_email self.name = name self.password = password self.current_job_title = current_job_title def change_password(self, new_password): self.password = new_password def change_job_title(self, new_job_title): self.current_job_title = new_job_title def get_user_info(self): print(f"User {self.name} currently works as a {self.current_job_title}. You can contact them at {self.email}")
post.py:
class Post: def __init__(self, message, author): self.message = message self.author = author def get_post_info(self): print(f"Post: {self.message} written by {self.author}")