Android的面向对象的编程 [英] android object oriented programming

查看:112
本文介绍了Android的面向对象的编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打打闹闹使用Eclipse在Android的一些基本的编程。目前,我希望通过一本书,与被写在书中的一些示例code打。

我注意到,在这个特殊的书,所有的例子至今参加主活动的步伐。我不认为这是很好的面向对象的编程习惯,因为我从一个传统的Java后台我。

这是移动平台的普遍做法?不应类都在自己的文件包含?


解决方案

  

应该不是类都包含在他们自己的文件?


不一定为Android 活动是一个特例类。如果您尚未完成,我建议你阅读应用程序基础并下应用程序组件 ... <对活动特别是部分/ p>


  

这是活动重新presents具有用户接口的单一屏幕。例如,电子邮件应用程序可能具有一个活动,显示新的电子邮件的列表,另一个活动撰写的电子邮件,以及用于阅读电子邮件的另一个活动。 虽然活动共同形成的电子邮件应用程序中一个有凝聚力的用户体验,每一个都是相互独立的。因此,不同的应用程序可以就这些活动中的任何一个(如果电子邮件应用程序允许)。例如,相机应用可以就在构成新的邮件的电子邮件应用程序的活动,以便为用户分享图片。


请注意,我用粗体已经强调文本的部分。关键在于,本身就是一个活动不是完整的应用程序,如果允许的话,任何第三方应用程序可能会调用活动在你的应用程序之一。因此,它是常见的,使一个活性作为自包含尽可能。一个具体的例子是像的AsyncTask 的,它提供的方法来使用的东西执行一个后台线程以及操纵UI - 筑巢延伸的AsyncTask 是很常见的,并简化了code的私有类。其中延长广播接收器嵌套类也是出于同样的原因常见的。

这是说,没有什么错误使用单独的Java类文件POJO辅助类,例如,它只是归结为你的应用程序的复杂程度,但它可能意味着服用的Andr​​oid类如何某些工作的特殊考虑 - 在的AsyncTask 类为一种特别是如果在一个单独的类文件中定义的,试试吧,你会明白我的意思。 : - )

I am fooling around with some basic programming in Android using Eclipse. I'm currently looking through a book and playing with some sample code that is written in the book.

I have noticed that in this particular book, all of the examples so far take pace in Main-Activity. I don't believe this to be very good Object Oriented programming practice as I am from a traditional Java background.

Is this the common practice for mobile platforms? Shouldn't classes all be contained in their own files?

解决方案

Shouldn't classes all be contained in their own files?

Not necessarily as an Android Activity is a 'special case' class. If you haven't done already, I'd recommend you read Application Fundamentals and in particular the section on 'Activities' under Application components...

An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others. As such, a different application can start any one of these activities (if the email application allows it). For example, a camera application can start the activity in the email application that composes new mail, in order for the user to share a picture.

Note the section of text that I've highlighted in bold. The point is that an Activity in itself is not the complete app and if allowed, any third-party app can potentially invoke an Activity in one of your apps. As such, it is common to make an Activity as self-contained as possible. One particular example is the use of something like an AsyncTask which provides methods to execute a background thread as well as manipulate the UI - nesting a private class which extends AsyncTask is quite common and simplifies code. Nesting classes which extend BroadcastReceiver is also common for the same reason.

That said, there is nothing wrong with using separate Java class files for POJO helper classes, for example, it just comes down to how complex your app is but it can mean taking special consideration of how certain Android classes work - the AsyncTask class being one in particular if defined in a separate class file, try it and you'll see what I mean. :-)

这篇关于Android的面向对象的编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆