View Full Version : Understanding super()
Slowburn
05-11-2005, 08:03 PM
Hi Everyone,
I'm not sure why I can't get my head around this, but was wonderign if anyone could give me some simple examples of exactly what super() does.
I understand that super() will invoke a function in the parent class, however,what is the usefullness of this?
Cheers.
deadbeat
05-11-2005, 08:15 PM
Super allows you to extend the functionality of a method in the subclass without overriding it entirely:
class A{
public function A(){
trace("this is the A constructor");
}
public function doStuff(){
trace("doStuff called from class A");
}
}
class B extends A{
public function B(){
trace("this is the B constructor");
}
public function doStuff(){
super.doStuff();
trace("doStuff called from class B");
}
}
var myObj=new B();
myObj.doStuff();
Comment out the call to super.doStuff() in class B, and you will see that you lose the functionality provided by the superclass...
K.
red penguin
05-11-2005, 08:22 PM
I would say look at page 193 - 197 in EAS 2.0....
In short, if I extend a class, the extended class will implicitly call the SuperClass's Construct. However if I need to pass an arg for the instantiation of the SubClass, I must call super() in the SubClass Construct with the needed argument....
super(myArg)
sleekdigital
05-11-2005, 08:22 PM
Here's how I would explain it...
When you create a subclass, you generally want to add functionality to an existing superclass. When an instance of the subclass is created (the contructor is executed), you probably want to execute the same stuff the superclass would in addition to some extra things that the subclass does. So you call the super class' constructor (by calling super()) instead of duplicating that code in the subclass's constructor. Does that make sense?
Slowburn
05-11-2005, 08:36 PM
sleekdigital,
That makes it more clear. Thank you.
So am I correct with this silly example:
class ReturnChildren extends XML {
var children:XMLNode;
function ReturnChildren( _xml:String ) {
this.init( _xml);
}
function init ( _xml:String ):XMLNode {
super( _xml);
return children = this.firstChild.childNodes;
}
}
/*EXAMPLE*//// Example Usage
var str = '<One><Two attr=attribute1>Hello World</Two></One>';
myChildren:ReturnChildren = new ReturnChildren( str );
trace(myChildren); // returns '<Two attr=attribute1>Hello World</Two>'
This class should return the children of an XML file.
sleekdigital
05-11-2005, 10:38 PM
First, I think there might be some special considerations when extending XML, but i don't recall for sure.
Second .. I'm not sure sure the specifics of what you are doing here is a good idea, but I think you have the general concept.
senocular
05-11-2005, 11:19 PM
Slowburn, the general idea behind that code is correct (though I dont think that particular script will work because of minor errors).
The problem with extending XML is that many methods associated with XML are not from the XML object, but, rather, XMLNode. XMLNode you cannot extend. Well, you can, but no XML object will make use of it because XMLNode instantiation is done internally within the XML object. It always uses XMLNode and there's really nothing you can do about that (that I'm aware of). This is where extending through prototyping can be helpful.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.