java - Why does a callback method needs to return something? -
i have never before seen such thing, let's have such listener object:
mywhateverlistener dafuqlistener = new mywhateverlistener() { @override public void onsuccesscall(string s) { // success call } @override public void onfailcall(boolean b) { // fail call } @override public boolean ondafuqcall(int i, boolean b) { // whatever code return false; } };
everything fine, appropriate method called to, when action succeedes, what's return
inside ondafuqcall
method, why needed, return something?
p.s. interface android ads provider's sdk.
it quite unclear ask i'll give shot.
interfaces way allow objects follow specific pattern. come handy, instance, when have interface called "listener" , 5 implementations: actionlistener, mouselistener, keylistener, closelistener, statechangelistener. if want have method allowing user register user, instead of having make separate "registerlistener" methods each implementation can have: registerlistener(listener l)
.
now post, interfaces may contain methods return values, example if have interface called event, , contains method called iscanceled()
returns boolean, if have implementation called "clickevent" , want check if event (after has been called) canceled or i'll invoke iscanceled()
method , should return value, because handled implementing object.
so wrap up, return value needed piece of code calls listener information. if in java interfaces , implementations can find ton of examples. can check looking java.uti.list interface source , implementation java.util.arraylist
more information can obtained docs:
https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
https://docs.oracle.com/javase/tutorial/java/iandi/createinterface.html
edit #1: here example explained above, represented in code:
the event interface:
package com.fillpant.examples; public interface event { public boolean iscanceled(); public void setcanceled(boolean value); }
the clickevent (that implements event):
package com.fillpant.examples; public class clickevent implements event { private boolean canceled = false; @override public boolean iscanceled() { return canceled; } @override public void setcanceled(boolean value) { canceled = value; } }
the place clickevent called. here demonstrate why return value needed (see iscanceled()
method):
package com.fillpant.examples; public class eventcaller { //this class calls event, , listeners have handle it; public void callclickevent(){ event e = new clickevent(); for(listener l : all_registered_listeners){//this hypothetical, if had listeners. l.event(e); } if(e.iscanceled()) return; else{ //dostuff } } }
if have question don't hesitate ask :d
Comments
Post a Comment