actionscript-3 - 如何在 Actionscript 3 中为我自己的类实现转换?

标签 actionscript-3 casting

我在 AS3 中创建了一个类来表示复数。它不继承任何东西。我怎样才能从例如数字? IE。我想要这个:

5 as Complex

与此相同:

new Complex(5);

我可以在我的类(class)中加入神奇的 cast() 函数吗?

最佳答案

恐怕不会。 Number 是与您的 Complex 类无关的类型。您将收到的只是:

1067: Implicit coercion of a value of type Number to an unrelated type Complex

我唯一能想到的就是做这样的事情:

asComplex(5);

public function asComplex(num:Number):Complex
{
    return new Complex(num);
}

但不确定这有什么意义。

关于actionscript-3 - 如何在 Actionscript 3 中为我自己的类实现转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7076292/

相关文章:

actionscript-3 - 如何在 Stage3D 中获得大于 2048 像素的纹理?

oracle - 如何在 View 中创建非空列

C++ 为什么我们需要显式地从一种类型转换为另一种类型?

c++ - 为什么需要强制转换为 bool 值?

actionscript-3 - 位图颜色 Replace() 阈值

actionscript-3 - 可以从 Flex Web 应用程序获取桌面通知吗?

java - AIR admob 问题 #3500 : The extension context does not have a method with the name cacheInterstitial

apache-flex - Sprite 未接收键盘事件

c++ - 为什么在我转换为 "operator bool()"时调用 "long"?

C++类成员函数指针指向函数指针