使用泛型的Java类转换

时间:2016-06-10 10:01:34

标签: java generics casting

我在以下代码中遇到Java类型不匹配。为了更好的阅读,我跳过了课堂主体。

class Main{

  //interface
  public interface HasDeletedFlag {
    public boolean deleted_flag();
  }

  //classes
  public abstract class Entity<? extends Entity.Factory<E, ?>>> {
    ...
  }

  public class Select{
    ...

    public <E extends Entity<? extends Entity.Factory<E, ?>>> List<E> search (E.Factory<E,?> result_factory){
      ...
    }
  }

  public static class Person extends Entity<Person.$.Factory>
     implements HasDeletedFlag {
    ...
    public static class $ {
      public static class Factory
        extends Entity.Factory<Person,Factory>{
        ...
      }
    }
  }

  static class Database{
    ...
    public final Person.$.Factory person = new Person.$.Factory();
  }

  //method
  private <E extends Entity<?> & HasDeletedFlag> List<E> findEntity(String id){

    ...
    Database db = new Database(); 
    List<E> objects = new ArrayList<E>();
    Select query = new Select();
    objects = query.<Person>search(db.person);

  }
}

在以下行中,我得到了类不匹配错误:

objects = query.<Person>search(db.person);

如果我添加类似(List<E>)的类强制​​转换,则错误消失,但在我的理解中,Person类型与E类型匹配。

问候 Goetz的

0 个答案:

没有答案