我db4o lib
用于存储数据。
例如,我有此代码用于存储数据(即新闻)
public static void insertNewsToDB(Context context, final News news){
final String dbPath = context.getDir("news", Context.MODE_PRIVATE) + "/" + DB4O_NAME;
ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);
try {
db.store(news);
db.commit();
} finally {
db.close();
}
}
这从数据库获取数据:
public static List<News> getNewsListFromDB(Context context){
final List<News> news = new ArrayList<News>();
final String dbPath = context.getDir("news", Context.MODE_PRIVATE) + "/" + DB4O_NAME;
ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);
try {
ObjectSet<News> result = db.queryByExample(News.class);
news.addAll(result);
} finally {
db.close();
}
return news;
}
我该如何创建方法,使其不依赖于我必须通过的类的类型。更清楚地,它不依赖于第二个参数是News,User还是其他。同样,当我想从数据库获取新闻时。
我读过有关泛型和强制转换对象的信息,但我仍然不知道如何做到这一点。
请帮忙。
泛型示例。
public class GenericsType<T> {
private T t;
public T get(){
return this.t;
}
public void set(T t1){
this.t=t1;
}
public static void main(String args[]){
GenericsType<String> type = new GenericsType<>();
type.set("Pankaj"); //valid
GenericsType type1 = new GenericsType(); //raw type
type1.set("Pankaj"); //valid
type1.set(10); //valid and autoboxing support
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句