Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我一直在阅读有关使用灵活属性而不是通过使用 Dictionary 或类似的东西来存储它们的强类型固定属性的信息。使用灵活属性的一个明显优势是您可以在运行时更改对象具有的属性。
这种行为有哪些有趣的用例?
一些相关文献:
一个好处是类似于 Apple 所说的 Cocoa 中的键值编码。它允许您集中您可能想要对属性执行的常规操作;例如,获取、设置等。通过这样做,您可以使用观察者模式来通知其他对象更改,而无需为您可能想要观察的每个对象中的每个属性编写样板代码。