java - I saw many object.addActionListen(this), what's the meaning of it? -
for example:
import acm.gui.*; import acm.program.*; import java.awt.event.*; import javax.swing.*; import acm.graphics.*; public class temperatureconverter extends program { private intfield fahrenheitfield; public void init() { fahrenheitfield = new intfield(32); fahrenheitfield.addactionlistener(this); } }
so assume know temperatureconverter
subclass of actionlistener
, fahrenheitfield.addactionlistener(this);
mean? cause i'm "ah, whole screen shows instance of temperatureconverter", , whats meaning of passing fahrenheitfield
actionlistener
?
no idea of classes are, should clarify confusion
- if
intfield
or of superclasses declareaddactionlistener(actionlistener al)
method, need pass "anactionlistener
". actionlistener
interface
, "anactionlistener
" in previous point any class or of superclasses implementactionlistener
interface or of subinterfaces.- if class "an
actionlistener
" same inaddactionlistener
called,this
keyword refers current instance of class.
in following examples, myclass
"an actionlistener
":
class directly implements interface
class myclass implements actionlistener
class directly implements subinterface
class myclass implements subactionlistener interface subactionlistener extends actionlistener
class subclass of class directly implements interface
class myclass extends supermyclass class supermyclass implements actionlistener
class subclass of class directly implements subinterface
class myclass extends supermyclass class supermyclass implements subactionlistener interface subactionlistener extends actionlistener
in these example supermyclass
direct superclass of myclass
, can anywhere inheritance hierarchy. same goes interface.
there redundant implementations such as
class myclass extends supermyclass implements actionlistener class supermyclass implements actionlistener
or other odd combinations.
temperatureconverter
subclass ofactionlistener
semantics, class can't subclass of interface (the opposite is, amusingly, not true).
Comments
Post a Comment