Why should compiler allow super-class of a parameter in a function

人盡茶涼 提交于 2020-01-06 03:08:04

问题


public class ChildClass extends MovieClip
{
   public function ChildClass()
   {
   }

    public function aChildClassFunction()
     {
     }

}

 public class AnotherChildClass extends MovieClip
{
   public function ChildClass()
   {
   }

    public function aChildClassFunction()
     {
     }

}

Some random function that asks for childClass ( But a lazy programmer just uses MovieClip class )

    public function setChildClassInstance(var mc:MovieClip) 
    {
// How come "mc" represent and be allowed as parameter ??? It's a super-class.. ie. structurally a sub-set of child-class.

          mc.aChildClassFunction(); //<< Would generate run-time error for 2nd function call. 

    }

The above function can be used for any MovieClip instance also

 var childClassInstance:ChildClass ;
 var anotherChildClassInstance:AnotherChildClass ; 

 setChildClassInstance( childClassInstance )   // <<<<<VALID,  .... NO COMPILER ERROR 

 setChildClassInstance( anotherChildClassInstance )  
  //<<< VALID, NO COMPILER ERROR ..  BUT WILL CAUSE RUNTIME ERROR !!!! 
//
//
//

I wonder, how come SUPER CLASS (MovieClip here) be allowed and represent themeselves as parameter for child-classes ( here: ChildClass, AnotherChildClass). Especially because, "SUPERCLASSES" actually are "SUB-SET" of their PARENT-SET , ie. their child-class.

Thanks


回答1:


With the following class you can assume the Animal has inherited all the properties of of MovieClip.

package  {
    import flash.display.MovieClip;
    public class Animal extends MovieClip{
        public function Animal(){
            // constructor code
        }
        public function growl():void{
            trace('growl')
        }
    }
}

So lets look at the inheritance tree of MovieClip

MovieClip->Sprite->DisplayObjectContainer->InteractiveObject->DisplayObject->EventDispatcher->Object

And now we can say

Animal->MovieClip->Sprite->DisplayObjectContainer->InteractiveObject->DisplayObject->EventDispatcher->Object<br>

When we type cast an instance of Animal to Sprite.

var sprite:Sprite = new Animal()

we lose the methods that are defined in Animal IE:growl
This is all fine as long as we do not try to access anything defined above in the inheritance tree.
Remember we can type cast down the tree not up.

Using the above class

var sprite:Sprite = new Animal()
sprite.growl() // this will error since sprite does not contain

Now here is an exception to the rule.
MovieClip is dynamic so type casting as MovieClip will not lose the added method(growl).

var mc:MovieClip = new Animal()
mc.growl() // traces growl

Dynamic classes by definition can not have an interfaces associated with them since an interface is a contract to a class that implements them.



来源:https://stackoverflow.com/questions/14649115/why-should-compiler-allow-super-class-of-a-parameter-in-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!