Flex Builder中的动作script3跟踪

| 这是代码,我正在使用Flex Builder 4.5,
<?xml version=\"1.0\" encoding=\"utf-8\"?>

<s:Application xmlns:fx=\"http://ns.adobe.com/mxml/2009\" 

           xmlns:s=\"library://ns.adobe.com/flex/spark\" 
           xmlns:mx=\"library://ns.adobe.com/flex/mx\" minWidth=\"955\" minHeight=\"600\">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
    <![CDATA[
        public var ss:Array = [\"a\", \"b\", \"c\"];
        trace(ss[0]);
        public var str:String = \"Good Luck\";
        trace(str);


    ]]>
</fx:Script>

</s:Application>
我在跟踪语句旁边出现红叉,故障是 \“ 1120:访问已定义的属性ss \” 我也尝试过评论行,但没有运气。我已经尝试使用3.5、4.1和4.5 sdk。 我想念什么吗?请指导!! (我尝试谷歌搜索,但没有任何反应) 提前致谢。 (更新了代码)     
已邀请:
public var ss:String
是字段声明,
trace(ss)
是代码执行流程中的动作。将
trace(ss)
放在适当的范围内(在函数aka方法中),它将被编译和执行而没有任何问题。     
我认为您对类成员属性和局部变量感到困惑。在方法内部,您只能声明局部变量,例如:
public function debugString() : void {
    // declare a local property, only this, \'debugString\' function can acess
    // or modify this property - it is deleted when this function finishes.
    var myString : String = \"Hello World\";
    trace(myString);
}
但是,似乎您正在尝试定义Class成员属性(因为您在声明属性的可见性(即:public))。
public class HelloWorld {
    // Define a Class member property; all functions in this Class will be
    // able to access, and modify it.  This property will live for as long as the
    // Class is still referenced.
    public var myString : String = \"Hello World\";

    public function debugString() : void {
        // notice how we did not declare the \'myString\' variable inside 
        // this function.
        trace(myString);
    }
}
请注意,只有在构造了类之后才能访问成员属性。因此,您最早(可以)访问它们的方法是在您的构造函数中,例如:
class HelloWorld {
    public var myString : String = \"Hello World\";

    // This will not work, because you can only access static properties
    // outside of a function body.  This makes sense because a member property
    // belongs to each instance of a given Class, and you have not constructed
    // that instance yet.
    trace(myString);

    // This is the Class Constructor, it will be called automatically when a
    // new instance of the HelloWorld class is created.
    public function HelloWorld() {
        trace(myString);
    }
}
您可能想做的是利用静态属性。它们与类成员属性不同,因为它们在给定类的所有实例之间全局共享。按照惯例,CAPS中定义了静态属性:
public class HelloWorld {
    // Here we define a static property; this is accessible straight away and you
    // don\'t even need to create a new instance of the host class in order to 
    // access it, for example, you can call HelloWorld.MY_STRING from anywhere in your
    // application\'s codebase - ie: trace(HelloWorld.MY_STRING)
    public static var MY_STRING : String = \"Hello World\";
}
    

要回复问题请先登录注册