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

  1. if intfield or of superclasses declare addactionlistener(actionlistener al) method, need pass "an actionlistener".
  2. actionlistener interface, "an actionlistener" in previous point any class or of superclasses implement actionlistener interface or of subinterfaces.
  3. if class "an actionlistener" same in addactionlistener 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 of actionlistener

semantics, class can't subclass of interface (the opposite is, amusingly, not true).


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -