简体   繁体   中英

more than one 'Extends'

public class MainActivity extends fragment

public class MainActivity extends Activity

The issue I have is that I need to extend Fragment to be able to use the 'pager_title_strip' but to use a list view that can read json, it also needs to be extended.

How can I extend both within the same class?

You can't, a class can only extend one other class.

It can however implement multiple interfaces, maybe that can help you in your design.

Abstractly spoken, multiple inheritance is not the only answer to such design problems, and in Java it was excluded by designers of Java (Gosling et.al.) because they saw it as inherently unsafe. Still in Java 8 multiple inheritance is only allowed for behaviour (see interface inheritance and new "default" methods), but not in state.

Alternatively, you can also think about composition or delegate patterns. From Joshua Bloch we know his recommendation "Favor composition for inheritance". I think this should be possible in your case.

There is FragmentActivity or ActionBarActivity class which may solve your problem!

http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html

The issue I have is that I need to extend Fragment to be able to use the 'pager_title_strip'

There is no requirement to use fragments at all with a ViewPager , let alone with a PagerTitleStrip or PagerTabStrip .

so saying that does this mean you aren't able to use a listview box that reads json and the pager title strip at the same time?

It is perfectly possible to "use a listview box that reads json and the pager title strip at the same time". However, you will need to create more than one Java class, in all likelihood, such as an Activity and a Fragment .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM