ActionScript - 构造函数内的设置参数?

发布于 2024-10-01 22:18:01 字数 319 浏览 0 评论 0原文

我设计了一个自定义类(或组件?),它扩展了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没︽人懂的悲伤 2024-10-08 22:18:01

在我看来,这归结为类实例化代码部分的易用性。
如果您有 14 个参数,要么全部设置,要么全部跳过,那么这可能是最好的解决方案。

var defaultRectangle:Rectangle = new Rectangle(); //square of 1 x 1)    
var customRectangle:Rectangle = new Rectangle(0,0,  2,0  2,2,  0,2);

但是如果 14 个参数中的一些是可选的,那么它会变得有点难以阅读,那么我认为要么使用单独的 getter/setter 更具可读性,要么使用 paramater 对象(模仿命名参数)

//which parameter means what?
var girlfriend:Girl = new Girl("Blue", 0, 0, "", "", 1.8, 0, "", 140); 

//named parameters 
var girlfriend:Girl = new Girl({eyeColor: "Blue", height:1.8, iq:140});

//setters
var girlfriend:Girl = new Girl();
girlfriend.eyeColor = "Blue";
girlfriend.height = 1.8;
girlfriend.iq = 140;

我个人尝试尽可能多地使用模型使用“视图”时尽可能
因此,如果您的对象是 Sprite,因此是 View。也许最好的方法是:

var friend:Person = new Person({name: "Ford Prefect"});
var profileView:ProfileView = new ProfileView(friend);
addChild(profileView);

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.

var defaultRectangle:Rectangle = new Rectangle(); //square of 1 x 1)    
var customRectangle:Rectangle = new Rectangle(0,0,  2,0  2,2,  0,2);

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)

//which parameter means what?
var girlfriend:Girl = new Girl("Blue", 0, 0, "", "", 1.8, 0, "", 140); 

//named parameters 
var girlfriend:Girl = new Girl({eyeColor: "Blue", height:1.8, iq:140});

//setters
var girlfriend:Girl = new Girl();
girlfriend.eyeColor = "Blue";
girlfriend.height = 1.8;
girlfriend.iq = 140;

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:

var friend:Person = new Person({name: "Ford Prefect"});
var profileView:ProfileView = new ProfileView(friend);
addChild(profileView);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文