1

为了说明我的问题。假设以下代码片段:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script>
    <![CDATA[
        import mx.controls.Button;

        private function createButton():void
        {
            var myButton:Button = new Button();
            myButton.label = "Foo";
            this.btncontainer.addChild(myButton);
            trace ("New Button Created [" + myButton.toString() + "]"); 
        }       
    ]]>
</mx:Script>

<mx:Button label="Create Button" click="createButton()" />
<mx:VBox id="btncontainer" />

</mx:Application>

这个脚本的行为应该是显而易见的。每次单击“创建按钮”按钮都会生成一个带有“Foo”标签的新按钮。代码做什么以及为什么这样做对我来说很有意义。我的问题是关于控制台输出。当我在调试模式下运行应用程序并单击“创建按钮”四次时,我在控制台中得到以下信息:

New Button Created [main0.btncontainer.Button15]
New Button Created [main0.btncontainer.Button19]
New Button Created [main0.btncontainer.Button23]
New Button Created [main0.btncontainer.Button27]

我的问题是附加到对象名称的数字来自哪里?例如 Button15、19、23、27... 等?背景中是否有某种数组来保存对象,这是一个索引值吗?它是某种内部计数器吗?这是某种指针值吗?至少在我的测试中,为什么它似乎总是遵循相同的模式 15、19、23、27 ......在这种情况下每次都被 4 分隔?

我从概念上理解这里发生了什么。生成一个新的 Button 对象并分配内存。每次单击“创建按钮”时,我都会创建 Button 类的新实例并将其作为子对象添加到 VBox 对象中。我只是好奇在创建对象时附加到对象的数字的含义或意义是什么?

4

1 回答 1

4

不要忘记,由于 Flex 是开源的,您可以在代码中跟踪这类事情。

我发现了一个NameUtil.displayObjectToString似乎负责创建 Flex 实例的可打印名称的函数。还有NameUtil.createUniqueName创造name财产的。

看一下代码,但基本上 createUniqueName 拆分getQualifiedClassName以仅获取类名而没有包详细信息。NameUtil 有一个静态计数器,然后将其附加到该名称的末尾。Button15您的应用程序创建的第 15 个 FlexSprite 也是如此。

displayObjectToString并不太复杂,只是它通过将名称连接到“。”上的父项来遵循组件链。


需要注意的一点是 UIComponent.as 中的注释:

/**
 *  ID of the component. This value becomes the instance name of the object
 *  and should not contain any white space or special characters. Each component
 *  throughout an application should have a unique id.
 *
 *  <p>If your application is going to be tested by third party tools, give each component
 *  a meaningful id. Testing tools use ids to represent the control in their scripts and
 *  having a meaningful name can make scripts more readable. For example, set the
 *  value of a button to submit_button rather than b1 or button1.</p>
 */
public function get id():String
{
    return _id;
}

它说:“这个值成为对象的实例名称”,虽然这似乎是真的,但我无法找出从 id 到 name 的分配发生在哪里。它可能在编译期间转换时从 MXML 生成的 AS3 代码中。

于 2009-08-26T12:50:28.533 回答