您的当前位置:首页>热点报道 > 正文

全球简讯:Cannot Reference “XxxClass.xxx” Before Supertype Constructor Has Been Called

  • 2023-06-15 13:16:32 来源:博客园


【资料图】

百度翻译:在调用超类型构造函数之前无法引用“XxxClass.xxx” ----- 我的理解:一个类的构造器方法还未执行的时候,我们无法使用类的成员属性或成员方法。

下面是此错误的示例代码

public class MyException extends RuntimeException {    private int errorCode = 0;        public MyException(String message) {        super(message + getErrorCode()); // compilation error    }    public int getErrorCode() {        return errorCode;    }}

IDE提示错误:

说说我怎么遇到这个问题的?

我有一个组件工具类。

1 @Slf4j 2 public class Many2OneProcessor { 3  4     private static ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(15); 5  6     /** 7      * 将多长时间的多次操作合并成1次,单位:秒 8      */ 9     private final long intervalSecond;10     /**11      * 每次处理多少条数据12      */13     private final int perBatchCount;14     /**15      * 批次处理逻辑代码16      */17     private final Consumer> yourBusinessCode;18     private final ...19 20     public Many2OneProcessor(long intervalSecond, Class tClass, Consumer> yourBusinessCode) {21         this(intervalSecond, Integer.MAX_VALUE, tClass, yourBusinessCode);22     }23 24     public Many2OneProcessor(long intervalSecond, int perBatchCount, Class tClass, Consumer> yourBusinessCode) {25         26         ...此处省略若干行27         28     }    29     30     public void produce(T t) {31         redisUtil.lSet(LIST_KEY, t, HOURS.toMillis(1));32         scheduledThreadPool.schedule(this::consumeMsg, intervalSecond, TimeUnit.SECONDS);33     }34 35     public void consumeMsg() {36         redisLockTemplate.execute(LOCK_KEY, TimeUnit.SECONDS.toMillis(intervalSecond - 1), false, () -> {37             38             ...39             40             List tList = new ArrayList<>(perBatchCount + 1);41             for (int j = 0; j < perBatchCount; j++) {42                 Object o = redisUtil.lPop(LIST_KEY);43                 if (o == null) break;44                 tList.add((T) o);45             }46             if (perBatchCount != Integer.MAX_VALUE && redisUtil.lGetListSize(LIST_KEY) > 0) {47                 scheduledThreadPool.schedule(this::consumeMsg, intervalSecond, TimeUnit.SECONDS);48             }49             50             ...51             yourBusinessCode.accept(tList);52             53         });54     }55 }

注意到其中的两处Integer.MAX_VALUE。这无形中提高了代码理解和维护(重点是前者)的成本。

于是,做点重构。改为下面这样,代码的可理解方面,更上一层楼。

public class Many2OneProcessor {    /**     * 每次处理多少条数据     */    private final int perBatchCount;    private static final int PER_BATCH_COUNT_DEFAULT = Integer.MAX_VALUE;        public Many2OneProcessor(long intervalSecond, Class tClass, Consumer> yourBusinessCode) {        this(intervalSecond, PER_BATCH_COUNT_DEFAULT, tClass, yourBusinessCode);    }        public void consumeMsg() {        ...                    if (perBatchCount != PER_BATCH_COUNT_DEFAULT && redisUtil.lGetListSize(LIST_KEY) > 0) {        ...    }}

注意,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

标签:

推荐阅读

全球简讯:Cannot Reference “XxxClass.xxx” Before Supertype Constructor Has Been Called

在调用超类型构造函数之前无法引用“XxxClass xxx”-----一个类的构造

机械助力小麦抢收 科技显著提升作业效率

日前,山西临汾尧都区的38万亩小麦进入夏收高峰期,当地调配600多台联

美国:松绑韩国、台湾芯片企业,专家:你继续禁,千万别松绑|天天要闻

近日,传出一则消息,那就是美国打算对韩国、台湾等企业,开设在中国大

“虚拟工厂”拓宽乡村就业路 当前速讯

“虚拟工厂”拓宽乡村就业路

信息:企业机房属于几类机房类别 机房温度和湿度要求

1、机房分为A、B、C、D等四种类型,不同类型的机房,正常温、湿度范围

猜您喜欢

【版权及免责声明】凡注明"转载来源"的作品,均转载自其它媒体,转载目的在于传递更多的信息,并不代表本网赞同其观点和对其真实性负责。亚洲公益网倡导尊重与保护知识产权,如发现本站文章存在内容、版权或其它问题,烦请联系。 联系方式:8 86 239 5@qq.com,我们将及时沟通与处理。

助学扶困