简体   繁体   English

父类中包含的子类

[英]Subclass contained in parent class

Looking through some source code for a Settings App I found something that ressembles what you see below, where there are child classes of a class mentioned within the original class. 通过查看设置应用程序的一些源代码,我发现了一些类似于您在下面看到的内容的地方,原始类中提到了该类的子类。 These classes are not abstract and have no body. 这些类不是抽象的,也没有正文。

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

In the actual app, there are buttons "My Color" and "My Connect" that each open up new activities (or fragments if the screen is dual pane). 在实际的应用程序中,有“我的颜色”和“我的连接”按钮,每个按钮都打开新的活动(如果屏幕为双窗格,则为碎片)。

So I have two questions: what is the use of declaring subclasses within a class itself - from an Object Oriented programming point of view - as shown above? 所以我有两个问题:从面向对象的编程角度来看,在类本身内部声明子类有什么用? And my second question is, if the classes are clearly empty and not abstract, but the end result is not empty, what is the use of these empty declarations? 我的第二个问题是,如果这些类显然是空的而不是抽象的,但最终结果不是空的,那么这些空声明的用途是什么?

EDIT 1 编辑1

As pointed out in the comment, the Android repo has a nearly identical setup. 正如评论中指出的那样,Android存储库具有几乎相同的设置。 See the link http://tinyurl.com/nbkv7zg (around line 1070) 请参阅链接http://tinyurl.com/nbkv7zg (第1070行附近)

I'd call this generally a bad design. 我通常将其称为不良设计。

Nested classes may be used as alternative to organizing them in a package, especially when their use is limited to or make only sense in the context of the containing class. 嵌套类可以用作在包中组织它们的替代方法,尤其是当它们的使用仅限于或仅在包含类的上下文中有意义时。 If they are used outside, refactor them out. 如果在室外使用它们,请将其重构。

As both classes are obviously empty, they are a complete waste and could be removed. 由于这两类显然都是空的,因此它们是完全的浪费,可以将其清除。 Instead, each instance of the parent class could be used in a different role 相反,父类的每个实例都可以以不同的角色使用

mySettings colorSettings = new mySettings();
mySettings connectSettings = new mySettings();

Btw. 顺便说一句。 starting the name of a class with lower case is a bad practice 以小写字母开头的班级名称是一个坏习惯

Here is Oracle's answer to your first question: 这是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. •它增加了封装:考虑两个顶级类A和B,其中B需要访问A的成员,否则将其声明为私有。 By hiding class B within class A, A's members can be declared private and B can access them. 通过将类B隐藏在类A中,可以将A的成员声明为私有,而B可以访问它们。 In addition, B itself can be hidden from the outside world. 另外,B本身可以对外界隐藏。

•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 来源: http//docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Without seeing the rest of the project's code, my best guess would be that those two classes, although empty, must be declared as required by some part of the PreferenceActivity parent class. 没有看到项目的其余代码,我的最佳猜测是这两个类虽然为空,但必须根据PreferenceActivity父类的某些部分的要求进行声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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