问题
I was learning the Room Database for Android app development through a tutorial and came across an abstract method being used without its implementation. Following are the files:
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; } }
The method getContactDao is being used in another file:
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; } }
Here is the ContactDao class:
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);
}
My question is what does the getContactDao method do? And shouldn't the abstract class be implemented before being used?
来源:https://stackoverflow.com/questions/48790457/abstract-method-used-without-implementation