本文介绍了安卓:AsyncTask的建议:私有类和公共类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发的一些Android应用的团队,我们已经使用在过去的2个不同的方法(一说我个人prefere,另一个是其他开发商prefers)。

I'm currently developing some Android Apps in a team and we've used 2 different approaches during the last months (one that i personally prefere, and the other that the other developer prefers).

尽管到目前为止的结果都是一样的,这让我想知道......我们应该:

Although so far the results are the same, this got me wondering...should we:

  • 使用AsyncTasks因为使用它们的活动内部私有类。
  • 或使用AsyncTasks作为接收活动的情况下单独公开课

是否有任何建议谷歌的途径的几点思考呢?

Are any of the approachs recommended by Google?

什么是您的经验,说这个(优势,劣势,问题)?

What does your experience say about this (advantages, disadvantages, problems)?

推荐答案

内部类有利于那些意思是私人或以某种方式紧密联系在一起封闭类重presenting对象。偶尔有技术方面的原因,使用内部类(例如,模拟闭包)。他们还减少空间的污染。

Inner classes are good for representing objects that are meant to be private or somehow intimately tied to the enclosing class. Occasionally there are technical reasons for using inner classes (e.g., simulating closures). They also cut down on namespace pollution.

内部类的一个缺点是,如果他们访问封装类的私有成员(字段或函数),编译器将生成访问函数来的成员。语言纯化论者会说这破坏了封装的是否是好事还是坏事。接入功能添加一些开销每个接入(这通常不是一个因素,但也有它是)。另一个缺点是,它使源文件更复杂,因此难以管理。 (我偶尔会被蜇编辑功能的内部类,而认为它是在外部类,反之亦然。)最后,内部类往往不被重用,而单独的类往往可以参数化有多种用途

One disadvantage of inner classes is that if they access private members (fields or functions) of the enclosing class, the compiler will generate accessor functions to those members. Language purists will argue whether this breaking of encapsulation is a Good Thing or a Bad Thing. The access functions add a bit of overhead to each access (which usually isn't a factor, but there it is). Another disadvantage is that it makes the source file more complex and therefore harder to manage. (I've occasionally been stung by editing a function in the inner class while thinking it was in the outer class, and vice versa.) Finally, inner classes tend not to be reusable, while separate classes can often be parameterized to have multiple uses.

这些优点和缺点都是从我的头顶。我敢肯定,其他人将有更多的想法。

These pros and cons are off the top of my head. I'm sure others will have additional thoughts.

更新:

在此谷歌IO 影片内AsyncTask的选项被清楚地标明了错误的选项。

In this Google IO video the inner AsyncTask option is clearly marked as wrong option.

这篇关于安卓:AsyncTask的建议:私有类和公共类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:25