How to extend 2 classes?

末鹿安然 提交于 2019-12-10 17:15:35

问题


This might be a silly question, but I'm struggling on how to make my class extend 2 classes at the same time. I'm trying to make a SERVICE that uses ListActivity. How could I do that?


回答1:


Java or android Doesn't Support Multiple Inheritance




回答2:


I assume you are coding in the Java programming language. Then the simple answer is: You don't. Java does not support deriving from multiple classes. Make your ListActivity contain a Service.

class MyService extends Service{
  ...
}

class MyList extends ListActivity{
    MyService service = new MyService();
}



回答3:


In Java you can't extend more than 1 class at the same time. However, you can implement more than 1 interface (but this is not about your task).

You should look for bound service




回答4:


No, it is not possible. You may need to re-architect your implementation.




回答5:


You can never actually extend 2 classes directly, you need to use a few tricks and make a few interfaces to get it to work. Here is a guide: http://csis.pace.edu/~bergin/patterns/multipleinheritance.html




回答6:


Assuming you mean this kind of Service, you cannot do this at all; irrespective of Java's lack of multiple inheritance. Services cannot also be Activities. You should create a Service, and a separate ListActivity, and use a binder to communicate between them.

When you do want to inherit the functionality of two classes, use the delegation pattern.



来源:https://stackoverflow.com/questions/9330649/how-to-extend-2-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!