代码之家  ›  专栏  ›  技术社区  ›  Jono

在java中生成列表<>?

  •  2
  • Jono  · 技术社区  · 14 年前

    嗨,可以列个单子吗?

    下面是超级类方法:

    protected void insertAllItemsToDb(List<Object> items, String table) {
            // open db and table
    
            database().beginTransaction();
            // clear all data from table
            clearTable(table);
            // call a insert statement to insert each column from an item
            for (Object object : items) {
                insertItem(object, table);
            }
            // close db
            database().endTransaction();
            database().close();
        }
    

    在这个子类中有一个override方法:我可以在这里很好地转换对象。

      @Override
        protected void insertItem(Object object, String table) {
    
            CalendarEventItem item = (CalendarEventItem) object;
            eventItemValue = new ContentValues();
    
            eventItemValue.put(LABEL_EVENTS_TITLE, item.getEventTitle());
            eventItemValue.put(LABEL_EVENTS_LOCATION, item.getEventLocation());
            eventItemValue.put(LABEL_EVENTS_DATE, item.getEventStartTime()
                    .getDate());
            eventItemValue.put(LABEL_EVENTS_TIME, item.getEventStartTime()
                    .getTime());
            eventItemValue.put(LABEL_EVENTS_TIMEZONE, item.getEventStartTime()
                    .getTimeZone());
    
            database.insert(TABLE_NAME_EVENTS, null, eventItemValue);
    
        }
    

    然后,我使用以下命令从超类调用该方法:

    events =  (List<CalendarEventItem>) items;
            insertAllItemsToDb(events, TABLE_NAME_EVENTS);
    

    但我收到一个编译错误,说你不能投它。关于如何在不重复insertAllItemsToDb()中看到的相同步骤和代码的情况下实现这一点的任何想法

    3 回复  |  直到 14 年前
        1
  •  6
  •   Sean Patrick Floyd    14 年前

    使用类型参数

    public abstract class BaseClass<T>{
    
        protected abstract void insertItem(T object, String table);
    
        protected void insertAllItemsToDb(List<T> items, String table) {
            //...
            for (T object : items) {
                insertItem(object, table);
            }
            //...
        }
    
    }
    

    现在您不需要任何类型的转换,一个孩子类只需要使用正确的类型:

    public class FooBar extends BaseClass<Phleem>{
        protected void insertItem(Phleem object, String table){
            // ...
        }
    
    }
    
        2
  •  5
  •   Andrzej Doyle    14 年前

    一个 List<Object> List<CalendarEventItem> ,所以编译器是正确的,它们是不可铸造的。原因很简单,下面是一个例子:

    final List<Object> listOne = new ArrayList<Object>();
    listOne.add("Hello World");
    
    final List<CalendarEventItem> listTwo = new ArrayList<CalendarEventItem>();
    listTwo.addAll(listOne); // Correctly disallowed by compiler
    
    // This is what you're trying to do
    List<CalendarEventItem> sneakyList = (List<CalendarEventItem>)listOne;
    listTwo.addAll(sneakyList);
    

    因此,不允许在两个不兼容类型之间进行强制转换,因为这样会破坏类型安全保证。

    你几乎肯定想宣布 insertAllItemsToDb 采取的方法 List<?> 列表<对象> ,因为只要元素类型是Object的子类,就不关心它是什么类型(这几乎是真的)。

    这样可以避免在不可转换的类型之间进行转换,而且通常使用起来更好。

    有关更多信息,请查看Angelika Langer's excellent的通配符边界部分 Java Generics FAQ 对于用于方法参数的集合,您可能应该使用通配符——唯一不会使用通配符的情况是,您同时读取和写入集合(这实际上非常罕见)。

        3
  •  0
  •   Bal    14 年前

    protected void insertAllItemsToDb(List items, String table) 第一行是: List<CalendarEventItem> newItems = (List<CalendarEventItem>) items ... 当然,在做这样的事情时,应该实现任何类型的检查/错误捕获。