简体   繁体   中英

Leave the <E> in ArrayList for the class to decide

I have an interface for a databasetable handler in my application. And one of the methods looks like this:

void add(ArrayList<? extends BaseObject> object);

And depending on which class that implements I want the to have different elemets.

For example, in one of my classes I want the following:

@Override
public void add(List<Category> object) {

}

And the class looks like this:

public class Category_Handler extends SQLiteOpenHelper implements HandlerInterface{

}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}


@Override
public void add(ArrayList<Category> object) {
    //Preparing the database variables
    SQLiteDatabase db = this.getWritableDatabase();

    for(int i = 0; i > objects.size(); i++){
        //Getting the values for the Category
        String name     = objects.get(i).getName();
        int subCategory = objects.get(i).getSubCat();

        if (db != null) {
            ContentValues cv = new ContentValues();

            //Adding values
            cv.put(Category_DAO.KEY_CATEGORY_NAME, name);
            cv.put(Category_DAO.KEY_SUBCATEGORY_TO, subCategory);

            //Finnish the insertion
            db.insert(Category_DAO.TABLE_NAME, null, cv);

        }
    }
db.close();
}

@Override
public void remove(int id) {

}

@Override
public void edit(int id, ArrayList<Category> values) {

}

And Category looks like this

public class Category extends BaseObject{
private String name;
private int subCat;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getSubCat() {
    return subCat;
}

public void setSubCat(int subCat) {
    this.subCat = subCat;
}

}

The problem I'm facing now is when I change to I get an error message saying "Method does not override method from its superclass.

Anyone know if there is any way of solving this? Is there even possible to have a solution like this?

Then you need to bind the type, you cannot use the wildcard ?.

We need to see more code, but maybe something like

class DatabaseTableHandler<E extends BaseObject>{

   public void add(List<E> object){}

 }

and then you can do

class CategoryHandler extends DatabaseHandler<Category>{

      @Override
      public void add(List<Category> object) {}

}

But if this method does what it seems to be doing (adding all from the list), the signature should probably be

      public void addAll(List<? extends E> object){}

so that you can also accept a List<SubCategory> . java.util.Collection#addAll also works like that.

How about a generic interface that uses the type of the add method as its paramter like so:

public interface DatabaseHandler<T extends BaseObject> {
    public void add(ArrayList<T> objects);
}

Each of your classes could then implement the desired interface version:

public class CategoryHandler implements DatabaseHandler<Category> {
    @Override
    public void add(ArrayList<Category> objects) {
        // stuff
    }
}

If you need to restrict the type of add in subclasses, you can't use a wildcard. But I suspect that you actually don't need to use any kind of fancy generics at all for this case -- why not just have all subclasses implement add(List<BaseObject> object) ? If add really just adds things to the list, then List<BaseObject> works just as well and comes with fewer headaches.

BaseObject is an abstract class so it has abstract methods that you have to implement. See The API of BaseObject to see the abstract methods that you have to implement. Your IDE should help you on this problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM