5

我正在android中学习SQLite。对于 SQLite,我指的是 developer.android.com。但是我在阅读代码时有些困惑。他们编写了FeedReaderContract构造函数来防止实例化FeedReaderContract类,但他们没有在任何地方定义类以及和FeedReaderContract之间的关系。FeedReaderContractFeedEntry

这是我所指的链接。我提供代码。如何在 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() {}
4

1 回答 1

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
}

所以你不能实例化合约,但可以访问所有内部类常量。就像示例行:

private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + FeedReaderContract.FeedEntry.TABLE_NAME + " (" +
FeedReaderContract.FeedEntry._ID + " INTEGER PRIMARY KEY," //continues

在 class 中访问内部类的FeedEntry常量 (TABLE_NAME和) 。_IDFeedReaderContract

希望能帮助到你。

于 2013-01-16T09:11:35.877 回答