问题
I want to display the same options menu on all of my application's activities. I created a generic Activity that implements the menu, and all my further activies extend it.
The problem: when I need to extend other specific activities, like ListActivity and MapActivity, I can't figure out how to extend the generic activity and add the List or Map behaviour to the new class. To deal with the issue I had to create three different generic activities, each extending Activity, ListActivity and MapActivity.
I've tried creating an abstract activity but it doesn't work, I would need to extend two classes at the same time. I could try interfaces but since I can't implement methods, I would have to paste the menu implementation all over the second-level classes, right?
回答1:
You can't do this. Java doesn't allow multiple inheritance.
When I need this kind of behavior and it depends on the Activity lifecycle I just replicate it two abstract classes:
AbstractActivity
AbstractMapActivity
You can also read more about multiple inheritance:
- Multiple Inheritance in Java
- Simulate multiple inheritance in Java
来源:https://stackoverflow.com/questions/3924464/how-to-implement-a-generic-activity-that-can-be-extended-by-normal-list-and-map