I am new to Java and I started to learn it on Android platform, I know its not good to start learning language on emulation of mobile platform, but anyway....
What I woud like to ask about java, is "external" calling of some methods. I mean, often in program, or tutorials, you just ovveride some method, and it gets run autmatically based on some action.
This is very nice actually, I really like it, but I would like to know how this this implemented. Does JVM has to implement those, or are they user-defineable somehow?
For axample on Android are methods surfaceCreated(),surfaceDestroyed()
which are called on respective event, and you than can handle it. The similiar are button click handling, and many more events.
I would just like to know how this is implemented, becouse, for example in C you have to manually check wheather some action happened or not. And you are limited by data provided by OS. So, does JVM has predefined actions it can call, or can you manually somehow tell it to do somthing based on somthing? I know this is strange question, but in fact its so strange to me I cannot explain it better. Maybe you can understand my not-knowing if you knew I programmed mainly for MCU in开发者_StackOverflow社区 C, so this behavior is strange to me. But I like it.
This is called Event-Delegation Model.
On occurrence of any event if listeners are registered, Proper delegates are called.
keep in mind that thing everything is oops in this and will be dealt in terms of classes and objects
We can understand this from a very simple Example say of a button click.
Consider i make this class
class MyButtonClickListener implements OnClickListener
{
public void onClick(View v)
{
//do something on button click
}
}
Now see this class is implementing a interface. This class has to provide body to the empty method of interface to implement it. else the code will not compile.
This ensures that every object of this class has a body of onClick Method. Now let us register this to listen our button click.
say my Button is button01
button01.setOnClickListener(new MyButtonClickListener());
now consider object button01 has a list maintained in it somewhere which has a address of object to do something later( new MyButtonClickListener() in our case).
now the layout managers are coded in a way that on a occurrence of a event (say button click) send this event to the object listener list to perform further action.
this will be happened in the fashion say when button is clicked then buttons list of listener is checked if it is found not null that means there is a listener. now on the reference found in the list , onClick Method is called. specifically onClick is called because we called the setOnCLickListener to set the listener. if you will check the code for this method. you will found method is receiving OnClickListener reference. this is a object of a class that implements the OnClickListener interface so must have provided a body to onClick method.
and thus this delegation is performed. this is simply oops. i hope i am able to explain it to a good level.
you ask about two distinct things:
some methods that you can override, which are called when some action happens (onResume(),...). They are always called (by the runtime/framework), and when your class overrides them, your implementation of that methods is called. But somewhere in the code is an actual call to this method. These are called virtual methods.
In the button click events you subscribe to are similar, but that is event-driven programming. When you subscribe to a button click event, for example
foo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
... do stuff
}
}
the foo
object saves the OnClickListener somewhere to its internals. When the button is clicked, it looks in its internals if it has any OnClickListeners saved, and if so, it invokes onClick() method in each of them.
精彩评论