java - 子类包含在父类中

标签 java android preferenceactivity

查看设置应用程序的一些源代码,我发现了一些类似于您在下面看到的内容,其中有原始类中提到的类的子类。这些类不是抽象的,也没有主体。

public class mySettings extends PreferenceActivity {
...
//Class definition
...
public static class myColorSettings extends mySettings {/*empty*/ }
public static class myConnectSettings extends mySettings {/*empty*/}
}

在实际的应用程序中,有“我的颜色”和“我的连接”按钮,每个按钮都会打开新的 Activity (如果屏幕是双 Pane ,则为 fragment )。

所以我有两个问题:从面向对象编程的角度来看,在类本身中声明子类有什么用处?如上所示? 我的第二个问题是,如果类显然是空的并且不是抽象的,但最终结果不为空,那么这些空声明有什么用?

编辑 1

正如评论中所指出的,Android 存储库具有几乎相同的设置。请参阅链接http://tinyurl.com/nbkv7zg (第 1070 行左右)

最佳答案

以下是 Oracle 对您的第一个问题的回答:

Why Use Nested Classes?

Compelling reasons for using nested classes include the following:

•It is a way of logically grouping classes that are only used in one place: 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.

•It increases 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.

•It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.

Source: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

在没有看到项目其余代码的情况下,我最好的猜测是,这两个类虽然是空的,但必须按照 PreferenceActivity 父类的某些部分的要求进行声明。

关于java - 子类包含在父类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30877855/

相关文章:

java - 从整数成员变量中减去的方法未按预期工作

java - Sobel 运算符不适用于矩形图像

横向方向的android preferenceactivity填充

java - Flash 范围和请求范围之间的区别

java - Maven 2.2.1 优化项目构建时间

java - 如何让android应用程序优先使用指示灯

android - 改造 java.net.ProtocolException : Expected ':status' header not present

android - 列表中包含 Jetpack 中项目的适合/包裹宽度的任何想法 compose

Android:如何在抽屉导航中实现设置页面?

android - SeekBarPreference/NumberPickerPreference ClassCastException 异常