靜態方法和泛型



   
   
  
  
          
  1. public class ResponseBean<T> {
  2. private int code;
  3. private String message;
  4. private String lang;
  5. private String accessToken;
  6. private String sign;
  7. private int source;
  8. private Date time;
  9. private T value;
  10. public ResponseBean() {
  11. }
  12. private ResponseBean(int code, T value) {
  13. this(code, value, "");
  14. }
  15. private ResponseBean(int code, T value, String message) {
  16. this.time = new Date();
  17. this.lang = "zh_Cn";
  18. this.source = 201;
  19. this.code = code;
  20. this.value = value;
  21. this.message = message;
  22. }
  23. private ResponseBean(int code, String message) {
  24. this.time = new Date();
  25. this.lang = "zh_Cn";
  26. this.source = 201;
  27. this.code = code;
  28. this.message = message;
  29. }
  30. public static <T> ResponseBean<T> error(String msg, T data) {
  31. return new ResponseBean<>( 200, data, msg);
  32. }
  33. 省略 get() set()方法
  34. }

靜態方法不能訪問類中定義的泛型,如果靜態方法中數據類型不確定,可以在方法上定義泛型。

即上例中<T>就是聲名一個泛型T,之后兩個T是在使用泛型T。

原文地址:https://blog.csdn.net/m18870420619/article/details/79929316


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM