Consider this (anonymous):
speakBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mTts.speak(words.getText().toString(), TextToSpeech.QUEUE_ADD, null);
}});
vs. this: (non-anonymous):
class MyOuterClass {
private class MyOnClickListener implements OnClickListener {
@Override
public void onClick(View view) {
mTts.speak(words.getText().toString(), TextToSpeech.QUEUE_ADD, null);
}
}
// later (inside some method)...
speakBtn.setOnClickListener(new MyOnClickListener());
}
Except for the fewer number of lines, is there any other advantage to the anonymous 开发者_StackOverflow中文版form?
Is there a performance advantage?
The anonymous inner class has advantage over the inner class (as in the question example code) in that it closes over the local variables of the method (although only final
locals are usable).
Generally an inner class can be easily converted into a method with anonymous inner class, which helps reduce verbosity. If you've got an inner class that is so large that you want to make it non-local, you might want to think about battling with your IDE to put it in a new file as an outer class.
(The is also local classes, which are normal named inner classes defined within a method and that close over locals.)
The advantage to non-anonymous is that you can reuse the class. I believe the only reason to use an anonymous inner class is brevity.
Things I like about named inner classes:
- They have a name. I find it easier to debug when I see
MyOuterClass$MyOnClickListener
in a stack trace instead ofMyOuterClass$1
, which is what you get with an anonymous inner class. - It sometimes helps readability to separate out the actual code for the inner class from the place you're using it. I especially like this if I'm already in a long method or indented more than a level or two.
To comment on your point about performance, anonymous inner classes get compiled into normal classes, so there should be no performance difference.
The advantage is the time you save as a developer for not having to type the extra keystrokes. :)
Oh, and it also prevents you from needlessly having to come up with a new name for everything (which may cause name collisions in some cases).
There is one disadvantage that pops out:
If you need more than one of the same inner class, you pretty much need to use an explicitly defined class. For what you describe, no you don't need one. But you may decide at a later date you need another object that does the same functionality, but is a different object.
The purpose of anonymous classes is make your required class as local. Anonymous classes are used when we are very much sure that a particular class A is the only consumer for a Class B and no where else this B class can be used .Then better we defined that class B as anonymous class inside Named class A. We are writing the required logic within the same class so avoiding the creation of object from outer side. It will easy to maintain in terms of code maintainability. So Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.
精彩评论