2010年2月16日 星期二

關於super及extends

  在child class的constructor中,一定要用super呼叫parent的constructor,否則會跑出"1203: 基底類別 Parent 中找不到預設的建構函式。"這樣的錯誤訊息。


  另外,在child class中,就算用的是super,也只能呼叫parent class中設為protected 和 private 的 functions 和 variables。














Parent.as



package{
    public class Parent{
        private var private_var:String;
        protected var protected_var:String;
        public var public_var:String;
        
        function Parent(a:String, b:String, c:String){
            private_var = a;
            protected_var = b;
            public_var = c;
            trace("call Parent's constructor");
        }
        private function private_func(){
            trace("call private_func");
        }
        protected function protected_func(){
            trace("call protected_func");
        }
        public function public_func(){
            trace("call public_func");
        }
    }
}




 














Child.as



package{
    public class Child extends Parent{
        function Child(){
            super("A","B","C");
            trace("call Child's constructor");
            //trace(super.private_var);
            trace(super.protected_var);
            trace(super.public_var);
            //super.private_func();
            super.protected_func();
            super.public_func();
        }
    }
}

 




沒有留言:

張貼留言