Я думаю, вы путаетесь между свойствами члена класса и локальными переменными.Внутри метода вы можете объявлять только локальные переменные, например:
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);
}
Однако может показаться, что вы вместо этого пытаетесь определить свойства члена класса (потому что вы объявляли видимость свойства (т.е.: 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";
}