静态方法和泛型

  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
posted @ 2019-09-17 16:08  星朝  阅读(3580)  评论(0编辑  收藏  举报