如何在 ActionScript 3 中声明可以是任意数据类型的参数?

发布于 2024-10-09 18:15:45 字数 118 浏览 3 评论 0原文

虽然这有效:

    public function func(settings)
    {
    }

它报告了警告,那么执行此操作的标准方法是什么?

Though this works:

    public function func(settings)
    {
    }

It's reporting a warning,so what's the standard way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

潇烟暮雨 2024-10-16 18:15:45

将其键入“*”或对象数据类型将起作用 - 但它确实不理想。可能有某种方法可以指定正确的数据类型。

一个答案是将输入键入所有可能输入的最具体的共同祖先 - 因此,例如,如果您知道它将是一个 DisplayObject,但有时它将是一个 Loader,有时它将是一个 Sprite,那么只需将其键入 DisplayObject,因为 Loader 和 Sprite 从技术上讲都是 DisplayObject。

另一种方法可能更“正确”一点,那就是将其输入到接口中。您的对象可以实现该接口,然后就万事大吉了。

如果您使用“*”或对象,那么您将放弃此时的所有调试能力。理想情况下,您希望确切地知道存在哪些对象以及它们在应用程序生命周期中的位置。每次你将某个东西作为一个物体投射时,你就放弃了 - 你是在说“然后它进入这个隧道并消失”,可以这么说。最好避免这种情况,特别是对于必须由其他人维护的大型项目。

Typing it to "*" or Object datatypes will work - but it's really not ideal. There is probably SOME way to specify a correct datatype.

One answer is to type the input to the most specific common ancestor of all possible inputs - so, for instance, if you know it's going to be a DisplayObject but sometimes it's going to be a Loader and sometimes it's going to be a Sprite, just type it to DisplayObject since both Loader and Sprite ARE technically DisplayObjects.

The other way is perhaps a bit more "correct", and that is to type it to an Interface. Your objects can implement that Interface, and then you're all set.

If you use "*" or Object then you are giving up all ability to debug at that point. Ideally you want to know exactly what objects exist and where they exist in the lifecycle of your application. Every time you cast something as an Object you're giving up - you're saying "And then it goes into this tunnel and disappears", so to speak. It's something best avoided, especially for larger projects that will have to be maintained by other people.

清晰传感 2024-10-16 18:15:45

您应该明确指定返回类型和参数类。这使得您的代码在 6 个月内变得更快、更容易阅读!

所以这将是:

public function saveSettings(settings:Array):void
{
}

或者任何可能是调用你的函数的适当的东西:)但不要只是称它为func..正确命名它:)

you should definitly specify the return type and parameter classes. this makes your code faster, and much much easier to read in 6 months!

So this would be:

public function saveSettings(settings:Array):void
{
}

or what ever might be the appropriate things to call your function :) But don't just call it func.. name it properly :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文