【发布时间】:2014-01-17 18:01:17
【问题描述】:
我应该在android中初始化我的类的正确方法是什么,该类称为Compilation,它的所有值都在db中。
我可以做到以下几点:
1
public Compilation(int id)
{
// get db singleton here and fill all values
// however I feel this is bad OO because nobody knows I am doing this
}
2
public Compilation(int id, SQLiteDatabase db)
{
// use the provided db to get the info
// however now all calling classes will have to get the db for me
}
3
// get all compilations at once
SQLiteDatabase db = DatabaseHelper.getInstance().getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM Compilation", null);
while(c.moveToNext())
{
// get all params here
Compilation comp = new Compilation (a,b,c,d,e);
}
public Compilation(a,b,c,d,e)
{
// just assign all the values given to private vars
}
The problem I see with this is that now the Compilation class is no longer so self contained, it needs another class to initialise it.
哪一种方法是正确的?
【问题讨论】:
-
所有都是“正确的”,这是广泛的。但
public Compilation(Cursor c)是我的方式。
标签: java android class constructor dependencies