简体   繁体   English

Java,通用和getAll

[英]java, generic and getAll

I'm trying to modify my code to avoid all the duplicate code, using generic. 我正在尝试使用泛型修改我的代码,以避免所有重复的代码。

I have a class DBBaseClass wich will be extended by some other class. 我有一个DBBaseClass类,它将被其他一些类扩展。 I'm trying to create a getAll method that return all the object of the subclass stored in a database. 我正在尝试创建一个getAll方法,该方法返回存储在数据库中的子类的所有对象。

 class abstract DBBaseClass {

  abstract readDataBase(int _id);

  public  <T extends DBBaseClass> List<T> getAll()
  {

 List<T> array = new ArrayList<T>();    

 ArrayList<Integer> myints = DBManager.request_database_for_ids(getIdField(), getTableName());

 T myTObject = new T();    // NOT WORKING  *************************

 for (int i=0;i<myints.size();i++) {        
        myTObject.read(myints.get(i));
        array.add(myTObject);
    }               

return array;

I have the errors : - Cannot instantiate the type T - The type T is not generic; 我有错误:-无法实例化类型T-类型T不是通用的; it cannot be parameterized with arguments <> 不能使用参数<>对其进行参数化

How can i instanciate my T subclass here ?? 我如何在这里实例化我的T子类?

What's the trick ?? 诀窍是什么??

Thanks. 谢谢。

EDIT : 编辑:

To explain more in details my goal : I have two classes SUbClassA and SubClassB that extends DBBaseClass. 为了更详细地说明我的目标,我有两个扩展DBBaseClass的类SUbClassA和SubClassB。

I want to a method : SubClassA.getAll() that returns an arraylist of SUbClassA objects. 我想要一个方法:SubClassA.getAll(),它返回SUbClassA对象的数组列表。 SubClassB.getAll() that returns an arraylist of SUbClassB objects. SubClassB.getAll()返回SUbClassB对象的数组列表。

At first, i created getAll in all my subClasses ... So, I'm trying now to put this getAll() in the DBBaseclass. 首先,我在我的所有子类中创建了getAll ...因此,我现在尝试将此getAll()放入DBBaseclass中。

It's easy to do if i want them to return List but i don't know how to tell the method "return me a list of this particular kind of subclass". 如果我希望他们返回List,这很容易做到,但是我不知道如何告诉方法“向我返回这种特定子类的列表”。

您想要使DBBaseClass通用,为此声明它是这样的:

abstract class DBBaseClass<T extends DBBaseClass<?>>

You only can instantiate a concret class but not T as it is only a type and not DBBaseClass as it is abstract. 您只能实例化一个concret类,但不能实例化T因为它只是一个类型,而不能实例化DBBaseClass因为它是抽象的。

What could you do: 你能做什么:

  • Define a concrete class extending DBBaseClass such as SubDBBaseClass 定义扩展DBBaseClass的具体类,例如SubDBBaseClass
  • Create a factory method for each sub-class that returns a concrete instance such as 为每个返回具体实例的子类创建一个工厂方法,例如

     protected T create() { return new SubDBBaseClass(); } 
  • Invoke this method in your DBBaseClass.getAll() method such as 在您的DBBaseClass.getAll()方法中调用此方法,例如

     T myTObject = create(); 

Perhaps you didn't know, but your are using the active record pattern that has its ups and downs. 也许您不知道,但是您正在使用活动记录模式 ,它有起有落。 I don't like the pattern and I would redesign the architecture. 我不喜欢这种模式,我会重新设计架构。 But these are only my 5 cents. 但是,这只是我的5美分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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