需求:增加人类,使用人对象发表文章
"""
文章有标题,作者,内容,可以保存,数据保存在类属性中
数据类型:可以保存数据
文章类型:
属性:标题、作者、内容
方法:保存
增加:
人的类型:
属性:姓名、年龄、性别
方法:发表文章
"""
class Database(object):
"""数据类型"""
article_dict = {}
class Person(object):
"""人的类型"""
def __init__(self, name, age, gender):
"""属性初始化"""
self.name = name
self.age = age
self.gender = gender
def publish(self):
"""发表文章"""
title = input("请输入文章标题:")
content = input("请输入文章内容:")
article = Article(title, self.name, content)
article.save()
class Article(object):
"""文章类型"""
def __init__(self, title, author, content):
"""实例属性初始化"""
self.title = title
self.author = author
self.content = content
def save(self):
"""保存文章数据"""
Database.article_dict[self.title] = self
per1 = Person("张三", 20, "男")
per1.publish()
for title, article in Database.article_dict.items():
print(f"标题:{title}\n 作者:{article.author}\n文章内容:{article.content}")