dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: overloading bug ?!!

  1. #1
    flashing vampire
    Join Date
    Dec 2001
    Posts
    52

    overloading bug ?!!

    howdy~

    this is a mx2004 problem. i found something very strange for constructors, i have a class named GamePlayer and another one named HomePlayer which derived from GamePlayer. the code for each class is right below:
    Code:
    // GamePlayer class~
    class GamePlayer
    {
    	public function GamePlayer()
    	{
    		init();
    	}
    
    	private function init():Void
    	{
    		trace("super init !");
    	}
    }
    
    
    // HomePlayer class~
    class HomePlayer extends GamePlayer
    {
    	public function HomePlayer()
    	{
    		super();
    		init();
    	}
    
    	private function init():Void
    	{
    		trace("sub init !");
    	}
    }
    and, when the movie runs it output something like this:
    sub init !
    sub init !

    no matter what we tried init method of HomePlayer would be invoked twice and the one in GamePlayer would never be touched. this must be a big problem, how to fix it is to invoke super.init() instead. private access in Flash is not the same as in Java so subclass can access properties and methods of its superclass, but note that in languages like Java this action is absolutely forbidden(try protected keyword in Java if u do want subclass to access superclass's properties). here is an example:
    Code:
    class HomePlayer extends GamePlayer
    {
    	public function HomePlayer()
    	{
    		super.init();
    		init();
    	}
    	......
    }
    but as i said this action is not permitted in other languages like Java because of it is private, and it also breaks encapsulation.

    waiting for ur suggestion.
    Last edited by quiteblack; 02-24-2005 at 04:24 AM.
    Never end on learning~

  2. #2
    Novemberain Master
    Join Date
    Sep 2004
    Location
    Void
    Posts
    390
    So why don't you init you properties in the subclass constructor without calling a private method?


    Never give up...

  3. #3
    flashing vampire
    Join Date
    Dec 2001
    Posts
    52
    Originally posted by Unsteady
    So why don't you init you properties in the subclass constructor without calling a private method?
    these may be reasons:
    1 with Java style it recommends we display interface(public members) before private members. with an init method we can put constructor to interface part and place init into so called black box part.
    2 different class have unique names for their constructors, but with a same name which can be easy to unsertand and help us to locate the code we need with ease;
    3 it's my habit which is part of my life so i dont wanna discard it.
    Last edited by quiteblack; 02-24-2005 at 04:57 AM.
    Never end on learning~

  4. #4
    Senior Member
    Join Date
    May 2001
    Posts
    1,838
    code:
    class HomePlayer extends GamePlayer {
    public function HomePlayer() {
    }
    private function init():Void {
    trace("super init !");
    }
    }


  5. #5
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    If it helps, Flash isn't really OOP. It puts on a nice facade, but encapsulation? With Flash? Not happening.

    Methods inherited by classes are each run in the scope of the subclass instance created. When super is called through the subclass, it's run as a part of the subclass so it recognizes only the init of that subclass. Since the init was overridden (not overloaded), the subclass init was the init that was used. Using super to get through to the superclass init is about the best you can do (and this only works to a certain extent given your chain of inheritence).

    Yes, this is a bad, bad thing, but it's what Flash has to offer so it can actually be a good idea to keep on hand private methods of a class interface so that subclasses can avoid problems that may arise as a result of this.

    This issue is known. Less conventional attempts at handling behaviors resulting from this have been attempted. For example, see:
    http://proto.layer51.com/d.aspx?f=1197

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center