【发布时间】:2019-03-17 15:42:13
【问题描述】:
我是构建 Python 项目的新手,所以请原谅可能在这里写下的任何错误方法。
两个 JSON 方案代表两个对象。它们被序列化为类并具有共同的属性。
例子:
class TwoWheelVeicle(object):
def __init__(self,v_family, v_subfamily):
self.Family = v_family
self.SubFamily = v_subfamily
self.OtherProp = "other"
class FourWheelVeicle(object):
def __init__(self,v_family):
self.Family = v_family
self.AnotherProp = "another"
def run_an_highway(vehicle):
if isinstance(vehicle,FourWheelVeicle):
return "Wrooom"
if isinstance(vehicle,TwoWheelVeicle):
if veichle.SubFamily in SubFams.NotAllowed:
return "ALT!"
else:
return "Brooom" #forgive me for the sound
class SubFams(object):
NotAllowed = ["Bicycle","50cc"]
Known = ["200cc","Motorbike"]
我不太确定整个程序:
- 我应该创建一个抽象父类吗?
- NotAllowed 存储是否正确?这是由于需要更改其内容(即从一些全局参数 JSON 序列化,它是一个#TODO)
..或者只是我不想做这些?
最后,代码不允许任何检查我序列化的属性是否正确(如果 SubFamily 未知怎么办?应该在解码器中检查吗?)。
非常感谢。
【问题讨论】:
标签: python class inheritance properties deserialization