Cannot Reference “XxxClass.xxx” Before Supertype Constructor Has Bee ...

篮之新喜  金牌会员 | 2023-6-15 12:09:40 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 901|帖子 901|积分 2713

百度翻译:在调用超类型构造函数之前无法引用“XxxClass.xxx” ----- 我的理解:一个类的构造器方法还未执行的时候,我们无法使用类的成员属性或成员方法。
 
下面是此错误的示例代码
  1. public class MyException extends RuntimeException {
  2.     private int errorCode = 0;
  3.    
  4.     public MyException(String message) {
  5.         super(message + getErrorCode()); // compilation error
  6.     }
  7.     public int getErrorCode() {
  8.         return errorCode;
  9.     }
  10. }
复制代码
IDE提示错误:

 
 

说说我怎么遇到这个问题的?
我有一个组件工具类。
  1. 1 @Slf4j
  2. 2 public class Many2OneProcessor<T> {
  3. 3
  4. 4     private static ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(15);
  5. 5
  6. 6     /**
  7. 7      * 将多长时间的多次操作合并成1次,单位:秒
  8. 8      */
  9. 9     private final long intervalSecond;
  10. 10     /**
  11. 11      * 每次处理多少条数据
  12. 12      */
  13. 13     private final int perBatchCount;
  14. 14     /**
  15. 15      * 批次处理逻辑代码
  16. 16      */
  17. 17     private final Consumer<List<T>> yourBusinessCode;
  18. 18     private final ...
  19. 19
  20. 20     public Many2OneProcessor(long intervalSecond, Class<T> tClass, Consumer<List<T>> yourBusinessCode) {
  21. 21         this(intervalSecond, Integer.MAX_VALUE, tClass, yourBusinessCode);
  22. 22     }
  23. 23
  24. 24     public Many2OneProcessor(long intervalSecond, int perBatchCount, Class<T> tClass, Consumer<List<T>> yourBusinessCode) {
  25. 25         
  26. 26         ...此处省略若干行
  27. 27         
  28. 28     }   
  29. 29     
  30. 30     public void produce(T t) {
  31. 31         redisUtil.lSet(LIST_KEY, t, HOURS.toMillis(1));
  32. 32         scheduledThreadPool.schedule(this::consumeMsg, intervalSecond, TimeUnit.SECONDS);
  33. 33     }
  34. 34
  35. 35     public void consumeMsg() {
  36. 36         redisLockTemplate.execute(LOCK_KEY, TimeUnit.SECONDS.toMillis(intervalSecond - 1), false, () -> {
  37. 37            
  38. 38             ...
  39. 39            
  40. 40             List<T> tList = new ArrayList<>(perBatchCount + 1);
  41. 41             for (int j = 0; j < perBatchCount; j++) {
  42. 42                 Object o = redisUtil.lPop(LIST_KEY);
  43. 43                 if (o == null) break;
  44. 44                 tList.add((T) o);
  45. 45             }
  46. 46             if (perBatchCount != Integer.MAX_VALUE && redisUtil.lGetListSize(LIST_KEY) > 0) {
  47. 47                 scheduledThreadPool.schedule(this::consumeMsg, intervalSecond, TimeUnit.SECONDS);
  48. 48             }
  49. 49            
  50. 50             ...
  51. 51             yourBusinessCode.accept(tList);
  52. 52            
  53. 53         });
  54. 54     }
  55. 55 }
复制代码
 
注意到其中的两处Integer.MAX_VALUE。这无形中提高了代码理解和维护(重点是前者)的成本。
于是,做点重构。改为下面这样,代码的可理解方面,更上一层楼。
  1. public class Many2OneProcessor<T> {
  2.     /**
  3.      * 每次处理多少条数据
  4.      */
  5.     private final int perBatchCount;
  6.     private static final int PER_BATCH_COUNT_DEFAULT = Integer.MAX_VALUE;
  7.    
  8.     public Many2OneProcessor(long intervalSecond, Class<T> tClass, Consumer<List<T>> yourBusinessCode) {
  9.         this(intervalSecond, PER_BATCH_COUNT_DEFAULT, tClass, yourBusinessCode);
  10.     }
  11.    
  12.     public void consumeMsg() {
  13.         ...
  14.         
  15.             if (perBatchCount != PER_BATCH_COUNT_DEFAULT && redisUtil.lGetListSize(LIST_KEY) > 0) {
  16.         ...
  17.     }
  18. }
复制代码
 
注意,PER_BATCH_COUNT_DEFAULT 需要定义为static。否则会出现上面的预编译错误。Cannot reference 'Many2OneProcessor.PER_BATCH_COUNT_DEFAULT' before supertype constructor has been called。另外,在重构过程中,我使用了一种方案,见下图,也出现了这个错误:Cannot reference 'Many2OneProcessor.perBatchCount' before supertype constructor has been called

 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

篮之新喜

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表