2016-04-14 79 views
-2

我有這兩個類,我需要在父類的子類filds中進行初始化。Java泛型語法設置器

public class A { 

    protected LazyFacade<E> list; 
    protected LazyDataModel<E> model; 
    protected MyObject myObject; 

    protected LazyDataModel<E> buildModel() { 
      lazyModel = new LazyDataModel<E>() { 
       public List<E> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) { 
        List<E> resultList = null; 
        try { 
         setRowCount((int) list.countObjects(myObject, filters)); 
         resultList = list.listObject(myObject, first, pageSize, sortField, sortOrder, filters); 
        } catch (PersistenceException e) { 
        } 
        return resultList; 
       } 
      }; 
     return lazyModel; 
    } 
    //getters setters 
} 

public class B extends A { 

    public B() { 
     this.list = new LazyFacade<OtherClass>(); **//is it correct way to instantiate the list???** 
     this.myObject = getObjectInDataBase(); 
     this.model = buildModel();     **//the model must have the same class of LazyFacade, in this case OtherClass. 
                //How could i do it?** 
    } 
} 
+0

有什麼問題嗎?你認爲'E'是什麼? – SLaks

回答

0

嘗試改變public class A {public class A<E> {

+0

OK丹尼爾,但我不想在B類裏面創建一個新的A 。 我需要的是初始化子類中的字段,因爲只有在子類中我會知道參數E類型。 This.list = new LazyFacade ();似乎有作品。但我怎麼能在子類中設置模型字段的E參數? – ulima69

+0

對不起,也許我不明白這個問題,但我認爲你需要在類聲明行中包含這個泛型,否則你會得到一個錯誤。此外,要實例化超類的字段,您可能需要將'this.model = buildModel();'更改爲'this.model = super.buildModel();' –

+0

嘗試更改B類爲 'public class B extends A { public B(){ this.list = new LazyFacade (); this.myObject = getObjectInDataBase(); this.model = buildModel(); } }' 然後實例化一個B對象: 'B test = new B <>();' –