2015-12-05 147 views
2

我發現的最接近的問題(Method Chaining: How to use getThis() trick in case of multi level inheritance)沒有直接回答我的問題。我目前正在使用嗎啡,並正在設立一項休息服務。我見過很多版本的如何實現這個,並決定這樣做。我完全接受建議,然而,學習如何解決這個問題,將有助於我的編碼實踐,至少我認爲它會。另外,我知道我的命名慣例可能會關閉,因此在修正我打開以及:)Java多級通用繼承

這裏是我的代碼:

      1st 
/*****************************************************************************/ 
public interface BaseRepository <T extends BaseEntity> { 
    /* Methods Here */ 
} 
public class BaseController<T extends BaseEntity> implements BaseRepository<T> { 
    public BaseController(Class<T> type) { 
     this.type = type; 
    } 
    /* Overridden Methods Here*/ 
} 
         2nd 
/*****************************************************************************/ 
public interface UserRepository<T extends UserEntity> extends BaseRepository<UserEntity> { 

    /* Methods Here */ 

} 

public class UserController<T extends UserEntity> extends BaseController<UserEntity> implements UserRepository<T> { 

    Class<T> type; 

    public UserController(Class<T> type) { 
     super(type); // <---- Error Here 
     this.type = type; 
    } 

最後,我會希望有一個StudentController,StaffController,等等從UserController繼承。 BaseController將成爲其他控制器(不是用戶)的父級。但是,我不知道如何去做這件事。我曾嘗試滿足

public UserController(Class<T> type) { 
     super(type); // <---- Error Here 
     this.type = type; 
    } 

超(型)UserEntity.class代替「型」,然而,將只允許Basecontroller返回UserEntity性能。任何解決方案如果有辦法......就像我說過的,我仍然在學習,並且在接受這個問題的其他建議或對這個特定問題的任何建議方面完全開放。非常感激!謝謝:)

回答

2

我想你打算爲

public class UserController<T extends UserEntity> extends BaseController<T> 
                implements UserRepository<T> { 

,因爲這將允許你一個Class<T>傳遞給超類。

+0

我不知道爲什麼我沒有這樣做...... 1個字符。 –

+0

@MichaelVillar,因爲泛型和它們的錯誤消息需要一些用處。前幾年,我經常被他們困惑,但那是前一陣子。 ;) –

+0

是的,我還是相當新的,我感謝幫助!謝謝! –