为什么 Actionscript 不支持重载?

发布于 2024-08-12 10:39:40 字数 97 浏览 4 评论 0原文

Action script是基于面向对象编程开发的,但是为什么不支持函数重载呢?

Flex 支持重载吗?

如果是,请用一个真实的例子简单解释一下。

Action script is developed based on object oriented programming but why does it not support function overloading?

Does Flex support overloading?

If yes, please explain briefly with a real example.

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

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

发布评论

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

评论(5

深白境迁sunset 2024-08-19 10:39:40

正如您所说,Action Script 不支持函数重载(因此甚至在 Flex 中也不支持)。

但这些函数可能具有默认参数,如下所示:

public function DoSomething(a:String='', b:SomeObject=null, c:Number=0):void

DoSomething 可以通过 4 种不同的方式调用:

DoSomething()
DoSomething('aString')
DoSomething('aString', anObject)
DoSomething('aString', anObject, 123)

这种行为可能是因为 Action Script 遵循 ECMA Script 标准。函数确实是对象的一个​​属性,因此,就像不能有两个同名的属性一样,也不能有两个同名的函数。 (这只是一个假设)

这是标准 ECMA -262(ECMAScript 语言规范)第 13 节(PDF 文件第 83 页)表示,当您声明类似

function Identifier(arg0, arg1) {
    // body
}

的函数时,为当前变量对象创建一个名为 Identifier 的属性 并且 value 等于这样创建的 Function 对象:

new Function(arg0, arg1, body)

所以,这就是为什么你不能重载函数,因为你不能有多个函数当前变量对象的同名属性

As you say, function overloading is not supported in Action Script (and therefore not even in Flex).

But the functions may have default parameters like here:

public function DoSomething(a:String='', b:SomeObject=null, c:Number=0):void

DoSomething can be called in 4 different ways:

DoSomething()
DoSomething('aString')
DoSomething('aString', anObject)
DoSomething('aString', anObject, 123)

This behavior maybe is because Action Script follows the ECMA Script standard. A function is indeed one property of the object, so, like you CAN'T have two properties with the same name, you CAN'T have two functions with the same name. (This is just a hypothesis)

Here is the Standard ECMA-262 (ECMAScript Language Specification) in section 13 (page 83 of the PDF file) says that when you declare a function like

function Identifier(arg0, arg1) {
    // body
}

Create a property of the current variable object with name Identifier and value equals to a Function object created like this:

new Function(arg0, arg1, body)

So, that's why you can't overload a function, because you can't have more than one property of the current variable object with the same name

淡墨 2024-08-19 10:39:40

值得注意的是,函数重载不是 OOP 习惯用法,而是一种语言约定。 OOP 语言通常具有重载支持,但这不是必需的。

正如 lk 所指出的,您可以用他显示的结构来近似它。或者,您可以这样做:

public function overloaded(mandatory1: Type, mandatory2: Type, ...rest): *;

此函数将需要前两个参数,然后将其余参数作为数组传递,然后您可以根据需要进行处理。这可能是更灵活的方法。

It's worth noting that function overloading is not an OOP idiom, it's a language convention. OOP languages often have overloading support, but it's not necessary.

As lk notes, you can approximate it with the structure he shows. Alternately, you can do this:

public function overloaded(mandatory1: Type, mandatory2: Type, ...rest): *;

This function will require the first two arguments and then pass the rest in as an array, which you can then handle as needed. This is probably the more flexible approach.

So尛奶瓶 2024-08-19 10:39:40

还有另一种方法 - 带有任何参数的函数都会返回任何内容。

public function doSomething(...args):*{
    if(args.length==1){
        if(args[0] is String){
            return args[0] as String;
        }
        if(args[0] is Number){
            return args[0] as Number;
        }
    }
    if(args.length==2){
        if(args[0] is Number && args[1] is Number){
            return args[0]+args[1];
        }
    }

}

There is another way - function with any parameters returns anything.

public function doSomething(...args):*{
    if(args.length==1){
        if(args[0] is String){
            return args[0] as String;
        }
        if(args[0] is Number){
            return args[0] as Number;
        }
    }
    if(args.length==2){
        if(args[0] is Number && args[1] is Number){
            return args[0]+args[1];
        }
    }

}
一场信仰旅途 2024-08-19 10:39:40

您不能重载,但您可以为参数设置默认值,这实际上是同一件事,但有时它确实迫使您提前计划您的方法。

没有实现的原因可能主要是 Adob​​e 设计和编写该语言的时间/投资回报问题。

You can't overload, but you can set default values for arguments which is practically the same thing, but it does force you to plan your methods ahead sometimes.

The reason it doesn't is probably mostly a time/return on investment issue for Adobe in designing and writing the language.

憧憬巴黎街头的黎明 2024-08-19 10:39:40

可能是因为 Actionscript 在运行时按函数名称查找函数,而不是在编译时按名称和参数存储它们。

此功能可以轻松地从动态对象中添加和删除函数,以及使用 object['functionName']() 按名称获取和调用函数的能力,但我认为它使得实现重载非常困难在不弄乱这些功能的情况下很难。

Likely because Actionscript looks up functions by function name at runtime, rather than storing them by name and parameters at compile time.

This feature makes it easy to add and remove functions from dynamic objects, and the ability to get and call functions by name using object['functionName'](), but I imagine that it makes implementing overloading very difficult without making a mess of those features.

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