add save and load func

This commit is contained in:
bridge
2025-11-11 19:48:18 +08:00
parent 0cb7eacee7
commit 9b870475bf
21 changed files with 1348 additions and 32 deletions

View File

@@ -121,3 +121,17 @@ class Age:
def __repr__(self) -> str:
"""返回年龄的详细字符串表示"""
return f"Age({self.age})"
def to_dict(self) -> dict:
"""转换为可序列化的字典"""
return {
"age": self.age,
"max_lifespan": self.max_lifespan
}
@classmethod
def from_dict(cls, data: dict, realm: Realm) -> "Age":
"""从字典重建Age"""
age_obj = cls(data["age"], realm)
age_obj.max_lifespan = data["max_lifespan"]
return age_obj