我在android中学习SQLite。对于SQLite,我正在使用developer.android.com。但是在阅读代码时我遇到了一些困惑。他们写了FeedReaderContract
构造函数来防止
实例化FeedReaderContract
类,但他们没有在任何地方定义FeedReaderContract
类以及FeedReaderContract
和FeedEntry
之间的关系。
Here's link。我提供代码。如何在openhelper类中定义内部类。有人能建议我这么好吗?。
例如,此代码段定义了单个表的表名和列名:
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";
...
}
为防止某人意外地实例化合同类,请为其指定一个空构造函数。
//Prevents the FeedReaderContract class from being instantiated.
private FeedReaderContract() {}
答案 0 :(得分:4)
我理解为:
public static class FeedReaderContract{
// Prevents the FeedReaderContract class from being instantiated.
private FeedReaderContract() {}
//The FeedEntry table definition
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";
...
}
//more tables definition
}
因此,您无法实现合同,但可以访问所有内部类Constants。像示例行一样:
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + FeedReaderContract.FeedEntry.TABLE_NAME + " (" +
FeedReaderContract.FeedEntry._ID + " INTEGER PRIMARY KEY," //continues
访问FeedEntry
类中内部类的TABLE_NAME
常量(_ID
和FeedReaderContract
)。
希望它有所帮助。