我创建了一个名为GeneraFriendsTable
的通用类
像这样的代码:
public abstract class GeneralFriendsTable {
public static String TABLE_NAME;
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
public static void onCreate(SQLiteDatabase db) {
String DATABASE_CREATE = "CREATE TABLE " + TABLE_NAME + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_UNAME + " TEXT, " + ");";
db.execSQL(DATABASE_CREATE);
}
public static void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}
}
我希望从此类扩展到强制初始化字段TABLE_NAME
。
例如:
public class NewFriendsTable extends GeneralFriendsTable {
public static final String TABLE_NAME = "leaved_friends";
}
主要问题是:
将onCreate()
使用扩展类中的TABLE_NAME
正常工作吗?
答案 0 :(得分:0)
在超类中创建一个抽象函数以获取表名:
public abstract class GeneralFriendsTable {
public abstract String getTableName();
public static void onCreate(SQLiteDatabase db) {
String DATABASE_CREATE = "CREATE TABLE " + getTableName() + ...
}
}
然后在具体子类中实现该函数:
public class NewFriendsTable extends GeneralFriendsTable {
public String getTableName() {
return "leaved_friends";
}
}