为什么我无法在已经扩展类的类中实现类

时间:2014-04-23 18:51:37

标签: java android eclipse

这是我的DBInfo类:

public class DBInfo  stars table
 static final String STARS = "stars";
 static final String FIRST_NAME = "first_name";
 static final String LAST_NAME = "last_name";

 static final String CREATE_STAR_TABLE = "CREATE TABLE " + STARS + 
                                                    "(" + _ID + " integer primary key autoincrement, " 
                                                        + FIRST_NAME + " text not null, "
                                                        + LAST_NAME + " text not null);";

 //stars in movies
 static final String STARS_IN_MOVIES = "stars_in_movies";
 static final String STAR_ID = "star_id";
 static final String MOVIE_ID = "movie_id";
 static final String CREATE_STAR_IN_MOVIE_TABLE = "CREATE TABLE " + STARS_IN_MOVIES + 
                                                        "(" + STAR_ID + " integer not null, "
                                                            + MOVIE_ID + " integer not null, " 
                                                            + "FOREIGN KEY (" + MOVIE_ID + ") REFERENCES "+ MOVIES +" (" + _ID + ") "
                                                            + "FOREIGN KEY (" + STAR_ID + ") REFERENCES "+ STARS +" (" + _ID + "));";

 //statistics table
 static final String STATISTICS = "statistics";
 static final String RIGHT = "right";
 static final String WRONG = "wrong";
 static final String CREATE_STATISTICS_TABLE = "CREATE TABLE " + STATISTICS + 
                                                    "(" + _ID + " integer primary key autoincrement, " 
                                                    + RIGHT + " int not null default 0, "
                                                    + WRONG + " int not null default 0);";

}

现在我想实现这个已经扩展SQLiteOpenHelper的类DBAdapter类

public class DbAdapter extends SQLiteOpenHelper implements DBInfo{

    private static final String DATABASE_NAME = "quizdb";
    private static final int DATABASE_VERSION = 9;

    private static final String MOVIES_FILE = "movies.csv";
    private static final String STARS_FILE = "stars.csv";
    private static final String STARS_IN_MOVIES_FILE = "stars_in_movies.csv";

    private static SQLiteDatabase mDb;
    private static Context mContext;

    public static SQLiteDatabase getSQLiteDatabase(Context ctx){
        if(mDb == null){
            new DbAdapter(ctx);
        }
        return mDb;
    }

我收到以下错误:

DBInfo类型不能是DbAdapter的超接口;超接口必须是接口

3 个答案:

答案 0 :(得分:1)

您无法实施class

DBInfo成为interface

答案 1 :(得分:1)

  

实现DBInfo

这不起作用,因为您只能实施 接口,而您的DBInfo 。所以你只能选择:

  • 将您的DBInfo类更改为接口

在您的情况下,唯一的方法是将其更改为接口 1 更改整个应用程序逻辑。顺便说一句,创建保持静态常量的接口是非常好的做法:

public interface DBInfo {

   public static final String STARS = "stars";
   public static final String FIRST_NAME = "first_name";
   public static final String LAST_NAME = "last_name";
}

1 因为Java不支持multi-inheritance

答案 2 :(得分:0)

您只能实现接口,而不能实现类。这看起来像你应该尝试使用合成而不是继承(在DBInfo中有一个DbAdapter的实例而不是继承它)。