我最近了解到,在Python 3中,为了最小化类的访问器方法的数量,您可以使用词典基本上只有一组访问器方法,如下所示:
def __init__(self, **kwargs):
self.properties = kwargs
def get_properties(self):
return self.properties
def get_property(self, key):
return self.properties.get(key, None)
Run Code Online (Sandbox Code Playgroud)
这似乎非常有用,我想在Java中应用类似的东西.我一直在研究可能具有多个属性的应用程序,创建和跟踪所有访问器方法可能很痛苦.是否有类似的策略我可以用于Java?