0

这是一个普遍的问题。在所有 android 示例中,您都可以找到适配器、任务或其他任何内部类。这是android的最佳实践吗?

我知道这可以更轻松地处理 ui 更改,但在另一个制作大文件的站点上。

android中是否有任何规则可以将它们封装在单独的文件中或将其作为内部类包含在内?

非常感谢。

4

1 回答 1

2

Its not Android but its Java

From Java Oracle Docs:

Why Use Nested Classes?

There are several compelling reasons for using nested classes, among them:

It is a way of logically grouping classes that are only used in one place.

It increases encapsulation.

Nested classes can lead to more readable and maintainable code.

Logical grouping of classes—If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.

Increased encapsulation—Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.

More readable, maintainable code—Nesting small classes within top-level classes places the code closer to where it is used.

于 2013-08-28T12:44:53.643 回答