0

尝试从其子包中的类访问默认包中的类时收到错误消息。任何人都可以帮我解决这个问题。

仅供参考,我的包结构是 A -> B。我的意思是文件夹“A”作为默认包,“B”作为子包。

提前致谢。

4

3 回答 3

0

只需创建一个类 A 的对象,并从其对象中调用类实例方法。

var classAObj:A = new A();
classObj.MethodA();
于 2012-09-05T05:31:29.857 回答
0

我认为您正在寻找的是 B 类扩展 A 类。在您的代码中看起来像这样:

package main
{
    class B extends A
    {
        // Code here...
    }
}

在包中包含代码通常不会影响功能,它更像是一种组织工具。internal(关键字除外。)

于 2012-09-05T05:33:47.460 回答
0

私有的、受保护的和公共的呢?我在其他答案中看不到任何解释,所以在这里。

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();
于 2012-09-05T05:58:40.533 回答