ActionScript - 构造函数内的设置参数?
我设计了一个自定义类(或组件?),它扩展了 Sprite,其构造函数有 15 个参数。仅第一个参数是必需的,其余 14 个参数已分配默认值。一切都是必要的。
除了第一个必需参数之外,每个参数实际上都是属性设置器。该类还包含公共 setter 和 getter 函数,允许在构造后运行时更改属性。
我已经在我的桌面(27 英寸屏幕)上编写了该类,并意识到当我在 13 英寸笔记本电脑上使用该类时可能会遇到问题 - 代码提示超出了屏幕边缘,因为它太长了。
当setter函数可用时,在构造函数中包含可选setter作为参数是正常/最佳实践,还是setter应该始终与构造函数分开?
i've designed a custom class (or component?) that extends Sprite whose constructor has 15 parameters. only the first parameter is required, while the remaining 14 have default values assigned. all are necessary.
each of the parameters, except for the first required parameter are actually property setters. the class also contains public setter and getter functions, allowing for property changes at runtime after construction.
i've written the class on my desktop (27" screen) and realized i may have a problem when i was using the class on my 13" laptop - the code hinting was extending past the edges of the screen as it was too long.
is it normal/best practice to include optional setters as parameters in the constructor when setter functions are available, or should setters always be separate from the constructor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,这归结为类实例化代码部分的易用性。
如果您有 14 个参数,要么全部设置,要么全部跳过,那么这可能是最好的解决方案。
但是如果 14 个参数中的一些是可选的,那么它会变得有点难以阅读,那么我认为要么使用单独的 getter/setter 更具可读性,要么使用 paramater 对象(模仿命名参数)
我个人尝试尽可能多地使用模型使用“视图”时尽可能
因此,如果您的对象是 Sprite,因此是 View。也许最好的方法是:
In my opinion it comes down to ease of use at the class instantiating part of the code.
If you have 14 parameters that you either all set or all skip then it's probably the best solution.
But if some of the 14 parameters are optional, it becomes a bit hard to read, then I think either the use of separate getter/setters is more readable, or a paramater object (mimicking named parameters)
I personally try to use Models as much as possible when working with "Views"
So if your object is a Sprite and thus a View. Maybe the best approach could be: