[英]How to access a Class in the default package from a sub package Class in Flash AS3.0
尝试从其子包中的类访问默认包中的类时收到错误消息。 谁能帮我解决这个问题。
仅供参考,我的包结构为A->B。我的意思是文件夹“ A”为默认包,“ B”为子包。
提前致谢。
只需创建一个Class A对象,并从其对象中调用类实例方法即可。
var classAObj:A = new A();
classObj.MethodA();
我认为您正在寻找的是B类来扩展A类。这在您的代码中看起来像这样:
package main
{
class B extends A
{
// Code here...
}
}
将代码包含在包中通常不会影响功能,它更是一种组织工具。 ( internal
关键字除外。)
私有的 , 受保护的和公共的呢? 我在其他答案中看不到任何解释,所以在这里。
class A
{
private var _password:String;
public var username:String;
protected var serverURL:String;
public function login():void
{
// some code
callServerForLogin();
}
protected function callServerForLogin():void
{
// some code
}
}
class B extends A
{
public function B()
{
var parentPassword = super._password;
// FAILS because private and accessible only inside class A
var parentUsername = super.username
// all ok in here, public property
var parentServerURL = super.serverURL;
// all ok, because it is protected
// also we can call super.login(); or super.callServerForLogin();
}
// IMPORTANT we are also allowed to override public and protected functions
override public function login():void
{
super.login();
// we call the parent function to prevent loosing functionality;
Alert.show("Login called from class B");
}
override protected function callServerForLogin():void
{
super.callServerForLogin();
// keep also parent logic
Alert.show("calling protected method from B");
}
}
// ---- Now considering you declare an object of type B you can do the following
var bObj:B = new B();
// access public properties and call public functions from both B and A
bObj.username = "superhero";
bObj.login();
// will get compile error for next lines
bObj.serverURL = "host.port";
bObj.callServerForLogin();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.