English 中文(简体)
引用类?
原标题:References classes?

引用类的正确方法是什么,在本例中是从同一个包引用的。下面的代码显示了类“Two”试图跟踪类“One”中变量的值。没有成功。跟踪返回未定义。

//Class One
package
{
import flash.display.Sprite;

public class One extends Sprite
     {
     public var myString:String = "My String";

     public function One()
           {
           new Two();
           }
     }
}


//Class TWO
package
{
public class Two
     {
     private var oneClass:Class = One; // <- is that right?

     public function Two()
           {
           trace(oneClass.myString);
           }
     }
}
最佳回答

类一中的myString不是静态的。所以它不属于类,而是属于实例。这就是您的代码无法工作的原因。尝试使用:

static public var myString:String = "My String";
问题回答

跟踪中存在错误(oneClass.myString)myString不是静态的。但除此之外,您还可以将One分配给oneClass

类型<code>Class</code>的引用不具有类或对象的任何属性,不是实例。它只是对该类的构造函数的引用。即:

public class One {
    static public var classValue = "Hello";
    public var instanceValue = "World!";
}

public class Two {
    public function Two() {
        trace( One.classValue );                 // this traces: Hello
        trace( One.instanceValue );              // this throws an Error
        var classReference:Class = One;
        trace( classReference.classValue );      // this throws an Error
        trace( classReference.instanceValue );   // this throws an Error
        var objectReference:One = new classReference();
        trace( objectReference.classValue );     // this throws an Error
        trace( objectReference.instanceValue);   // this traces: World!
    }
}

类属性(静态属性)只能直接从类(如One.classValue)加入,而不能从class引用加入,实例属性(非静态属性)也只能从该类的对象加入(如new One().instanceValue





相关问题
Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...