我已在文档类“Main.as”中设置了一个变量。我现在正在尝试访问该变量并从其他类和函数中读取其值,获取该值并通过电子邮件发送它。
例如,在我的“Main.as”文件中,我有这个功能:
public var _myVar:String;
function create() {
_myVar = "hello";
}
现在来自我的其他课程“EmailtoFriend.as”我有一个新功能来尝试获取该预设变量的值:
function getVar() {
trace(_myVar);
}
为什么不输出“你好”?相反,我得到一个错误说:访问未定义的属性_myVar。如果我能让这个简单的例子起作用,我想它会帮助我理解很多东西。谢谢!
答案 0 :(得分:1)
除非明确指定,否则所有变量都隐式具有目标。没有显式目标的变量通常会查找函数的本地范围(在本例中为getVar()
)和类的全局范围(在本例中为EmailToFriend
)。
我认为在代码中不存在这些,从错误来判断。您将需要类似以下内容来访问var:
function getVar() {
var main:Main = new Main();
main.create();
trace(main._myVar);
}
答案 1 :(得分:1)
你得到的错误真的说明了一切。虽然_myVar
类Main
中定义了public var _myVar:String;
,但您的Emailtofriend
课程中未定义_myVar
。如果您想要访问Main
,则需要执行以下操作之一:
将this
对象的引用(使用EmailToFriend
)解析为package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public var _myVar:String;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
public function create():void
{
_myVar = "hello";
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
create();
var emailToFriend:EmailToFriend = new EmailToFriend(this);
emailToFriend.getVar();
}// end function
}// end class
}// end package
internal class EmailToFriend
{
private var _main:Main;
public function EmailToFriend(main:Main)
{
_main = main;
}// end function
public function getVar():void
{
trace(_main._myVar);
}// end function
}// end class
类:
Main.as(文档类):
_myVar
或者使Main
成为Main._myVar
的公共静态属性,并通过package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public static var _myVar:String;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
public function create():void
{
_myVar = "hello";
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
create();
var emailToFriend:EmailToFriend = new EmailToFriend();
emailToFriend.getVar();
}// end function
}// end class
}// end package
internal class EmailToFriend
{
public function EmailToFriend() {}
public function getVar():void
{
trace(Main._myVar);
}// end function
}// end class
访问它:
Main.as(文档类):
package
{
import EmailToFriend;
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public static var _myVar:String;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
public function create():void
{
_myVar = "hello";
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
create();
var emailToFriend:EmailToFriend = new EmailToFriend();
emailToFriend.getVar();
}// end function
}// end class
}// end package
另外一件小事,当使用下划线作为类属性时,您应该只将它们用于私有属性,而不是公共属性。好吧,我只说 ,但我的意思是它更常见。
<强> [UPDATE] 强>
这是对你评论的回应:
Main.as :
package
{
import Main;
public class EmailToFriend
{
public function EmailToFriend() {}
public function getVar():void
{
trace(Main._myVar);
}// end function
}// end class
}// end package
<强> EmailToFriend.as 强>:
{{1}}
答案 2 :(得分:0)
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public var _myVar:String;
public function Main(){
create();
}
private function create() {
_myVar = "hello";
}
}
}
}
在EmailtoFriend.a
import Main.as
var myMain = new Main();
trace(myMain._myVar);
答案 3 :(得分:0)
假设Main.as是您的文档类:
public var _myVar:String;
public function create():String {
//we specify that this function will return a String using the :String in the above line
//now give the variable a value
_myVar = "hello";
//and send it back to the caller of the function
return _myVar;
}
在你的其他课程中
function getVar():void {
//MovieClip(root) is another way of referencing your document class.
trace(MovieClip(root).create());
}
... OR
public var _myVar:String;
public function create():void {
//now give the variable a value
_myVar = "hello";
}
在你的其他课程中
function getVar():void {
//call the function that actually gives myVar a value
MovieClip(root).create();
//now you can trace the value
trace(MovieClip(root)._myVar);
}
答案 4 :(得分:0)
你应该做的是采用OOP方法,这意味着在你的类中使用封装。如果你不知道这意味着什么,那就没关系。例如,如果你有一个你想要访问的变量,那么你应该把它变成私有的,并设置它自己的返回变量的公共函数。像这样:
package {
public class SomeClass {
private var someVar:Number = 12; // A private variable, which means only this class can
// use the reference someVar, and only other outiside classes can use the function getSomeVar.
... // skip some regular class stuff
public function getSomeVar():Number {
return this.someVar; //returns the variable someVar from this class to whoever is accessing it.
//This function is public which means that anyone can call it and get the variable someVar.
}
}
}
要访问该变量,只需引用一个类实例:
var someClass:SomeClass = new SomeClass(); // create the instance using the variable someClass
var myVar:Number = someClass.getSomeVar(); // ACCESSES the variable that you want from the class,
//by first using the class instance reference, and then calling its public function that returns the value you want.