[英]What is the actual purpose of that nested class?
I am a newbie in Android programming and have little confusion in following code. 我是Android编程的新手,在以下代码中几乎没有混淆。 This portion of code is from android developer site: 这部分代码来自android开发者网站:
public final class FeedReaderContract {
// To prevent someone from accidentally instantiating the contract class,
// give it an empty constructor.
public FeedReaderContract() {}
/* Inner class that defines the table contents */
public static abstract class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "entry";
public static final String COLUMN_NAME_ENTRY_ID = "entryid";
public static final String COLUMN_NAME_TITLE = "title";
public static final String COLUMN_NAME_SUBTITLE = "subtitle";
...
}
}
I read some articles about nested classes in Java documentation and some other blogs and understood following reasons to use nested class: 我在Java文档和其他博客中读了一些有关嵌套类的文章,并了解使用嵌套类的以下原因:
But here, I could not figure out anything to use nested class. 但是在这里,我无法弄清楚使用嵌套类的任何方法。
What exactly is the purpose of making nested class? 进行嵌套类的目的到底是什么?
This particular nested class is used as a namespace for String
constants related to its main class. 这个特定的嵌套类用作与其主类相关的String
常量的名称空间 。
abstract
, so it's not intended for instantiation 它是abstract
,因此不适合实例化 BaseColumns
interface, which is essentially a way to "dump" string constants into the FeedEntry
class 它实现了BaseColumns
接口,这实际上是一种将字符串常量“转储”到FeedEntry
类的方法。 Why did they used nested class instead of free-standing class? 他们为什么使用嵌套类而不是独立类?
Use of a nested class underscores "semantic closeness" of the inner class to its outer class. 嵌套类的使用强调了内部类与其外部类的“语义紧密性”。 It cnveys to the readers of your code that FeedEntry
can be interpreted only in the context of FeedReaderContract
. 它向您的代码读者传达了FeedEntry
只能在FeedReaderContract
上下文中进行解释。 Otherwise, FeedEntry
is meaningless. 否则, FeedEntry
是没有意义的。 Essentially, this is done for the human readers of code, because in this case there would be no difference to the compiler. 本质上,这是为代码的人工阅读者完成的,因为在这种情况下,编译器不会有任何区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.