怎么会有人不小心在 Java 中发起了 class?
How can someone accidentally initiate a class in Java?
我在阅读 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";
...
}
}
这是教程的 link:
https://developer.android.com/training/basics/data-storage/databases.html#DbHelper
虽然我之前在 Java 上工作过,但我不明白为什么会在 Java 中意外启动 class。这怎么可能?
This 不是我要找的。
我认为它应该是私有的,例如:
private FeedReaderContract() {}
这将使开发人员无法实例化 FeedReaderContract 实例
我在阅读 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";
...
}
}
这是教程的 link: https://developer.android.com/training/basics/data-storage/databases.html#DbHelper
虽然我之前在 Java 上工作过,但我不明白为什么会在 Java 中意外启动 class。这怎么可能?
This 不是我要找的。
我认为它应该是私有的,例如:
private FeedReaderContract() {}
这将使开发人员无法实例化 FeedReaderContract 实例