如何在 ActionScript 3 中声明可以是任意数据类型的参数?
虽然这有效:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 * 或 Object :
http://www.adobe.com /livedocs/flash/9.0/ActionScriptLangRefV3/specialTypes.html#*
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3 /Object.html
You can use * or Object :
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/specialTypes.html#*
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/Object.html
将其键入“*”或对象数据类型将起作用 - 但它确实不理想。可能有某种方法可以指定正确的数据类型。
一个答案是将输入键入所有可能输入的最具体的共同祖先 - 因此,例如,如果您知道它将是一个 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.
您应该明确指定返回类型和参数类。这使得您的代码在 6 个月内变得更快、更容易阅读!
所以这将是:
或者任何可能是调用你的函数的适当的东西:)但不要只是称它为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:
or what ever might be the appropriate things to call your function :) But don't just call it func.. name it properly :)