我正在通过教程学习Android应用程序开发的房间数据库,并且遇到了一个没有实现的抽象方法。以下是文件:
AppDatabase.java
@Database(entities = {Contact.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
private static final String DATABASE_NAME = "database.db";
private static AppDatabase database;
**public abstract ContactDao getContactDao();**
public static AppDatabase getInstance(Context context) {
if(database == null)
{
database = Room.databaseBuilder(context, AppDatabase.class, DATABASE_NAME)
.allowMainThreadQueries().build();
}
return database;
}
}
方法getContactDao正在另一个文件中使用:
ContactsFragment
public class ContactsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_contacts, container, false);
AppDatabase database = AppDatabase.getInstance(getContext());
**ContactDao contactoDao = database.getContactDao();**
return view;
}
}
这是ContactDao类:
public interface ContactDao {
@Query("SELECT * FROM contact")
List<Contact> getAll();
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Contact contact);
@Update
void update(Contact contact);
@Delete
void delete(Contact contact);
}
我的问题是getContactDao方法有什么作用?并且在使用之前不应该实现抽象类吗?