2013-10-09 43 views
0
private Service generateActionResponse(@Nonnull Class<? extends RetryActionResultDto> response) { 
     if (response.isSuccess()) { 
      ... 
     } else if (response.getRetryDecision() { 
      .... 
     } 
    } 

public interface RetryActionResultDto extends DTO { 

    public RetryDecision getRetryDecision(); 

    public boolean isSuccess(); 

} 

,但我得到異常Class <?延伸RetryActionResultDto>是未定義的類型CLASS

的方法isSuccess()是未定義的類型類

我可以做什麼?

+2

你的迴應是Class的一個實例not RetryActionResultDto –

+0

嗯所以我能做什麼?在這個方法中,我想使用實現RetryActionResultDto接口的所有類。接口 – senzacionale

+0

Harsha R的答案是正確的,他將採用實現RetryActionResultDto的類的任何實例爲 –

回答

1

您可以重新編寫方法定義爲這樣的:

private <T extends RetryActionResultDto> String generateActionResponse(
      T response) { 
.. 
} 

它說,該方法的參數接受的RetryActionResultDto或其子類的實例。

+0

感謝您的解釋 – senzacionale

+0

@senzacionale歡迎您。 –

3

你的論點是一個類..不是該類的一個實例。因此錯誤。

嘗試將其更改爲:

private Service generateActionResponse(@Nonnull RetryActionResultDto response) { 
    if (response.isSuccess()) { 
     ... 
    } else if (response.getRetryDecision() { 
     .... 
    } 
} 

一個子類的實例也將通過它。

+0

應該是通用的,可以使用實現RetryActionResultDto的所有類。 – senzacionale

+0

@senzacionale:所有實現RetryActionResultDto的類都可以使用下面的harsha代碼.. –

+0

感謝您的解釋 – senzacionale

1
private <T> Service generateActionResponse(@Nonnull T extends RetryActionResultDto response) { 
    if (response.isSuccess()) { 
     ... 
    } else if (response.getRetryDecision() { 
     .... 
    } 
} 

但是,因爲RetryActionResultDto是interfce,該方法只接受參數它們是RetryActionResultDto亞型,即使沒有泛型。

+0

感謝您的解釋 – senzacionale

0

你在這裏試圖做的是錯誤的。 Class<? extends RetryActionResultDto>Class而不是實現RetryActionResultDto的類的對象。

如果你想要的對象,其類實現RetryActionResultDto作爲參數傳遞,那麼你可以使用

private Service generateActionResponse(@Nonnull RetryActionResultDto response) { 

作爲傳遞的參數已經實現了它會在界面和實際聲明的所有方法的接口方法的實現將在運行時相對於傳遞的對象被調用。

相關問題