首页 > 其他分享 >Cannot Reference “XxxClass.xxxmember” Before Supertype Constructor Has Been Called

Cannot Reference “XxxClass.xxxmember” Before Supertype Constructor Has Been Called

时间:2023-06-17 10:03:29浏览次数:39  
标签:yourBusinessCode Reference XxxClass perBatchCount private int intervalSecond Con

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

百度翻译:在调用超类型构造函数之前无法引用“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提示错误:Cannot reference 'MyException.getErrorCode' before supertype constructor has been called.

Cannot Reference “XxxClass.xxxmember” Before Supertype Constructor Has Been Called_构造函数

 

 

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

我有一个组件工具类。

1 @Slf4j
 2 public class Many2OneProcessor<T> {
 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<List<T>> yourBusinessCode;
18     private final ...
19 
20     public Many2OneProcessor(long intervalSecond, Class<T> tClass, Consumer<List<T>> yourBusinessCode) {
21         this(intervalSecond, Integer.MAX_VALUE, tClass, yourBusinessCode);
22     }
23 
24     public Many2OneProcessor(long intervalSecond, int perBatchCount, Class<T> tClass, Consumer<List<T>> 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<T> 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<T> {
    /**
     * 每次处理多少条数据
     */
    private final int perBatchCount;
    private static final int PER_BATCH_COUNT_DEFAULT = Integer.MAX_VALUE;
    
    public Many2OneProcessor(long intervalSecond, Class<T> tClass, Consumer<List<T>> 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.xxxmember” Before Supertype Constructor Has Been Called_List_02

 


当看到一些不好的代码时,会发现我还算优秀;当看到优秀的代码时,也才意识到持续学习的重要!-




标签:yourBusinessCode,Reference,XxxClass,perBatchCount,private,int,intervalSecond,Con
From: https://blog.51cto.com/u_15708799/6504552

相关文章

  • Weak References in .NET
    今天看到一个老外,用C#代码讲WeakReferences的用法不过我发现它的例子应该是用毛病的,在它的例子中weakRef应该没有逃开作用域,不能被正确回收,所以例子的结果也是不准的末尾给出了我对其修改后的例子,给出了两种作用域的对比DecidingWhentoUseWeakReferencesin.NET ......
  • Cannot Reference “XxxClass.xxx” Before Supertype Constructor Has Been Called
    百度翻译:在调用超类型构造函数之前无法引用“XxxClass.xxx”-----我的理解:一个类的构造器方法还未执行的时候,我们无法使用类的成员属性或成员方法。 下面是此错误的示例代码publicclassMyExceptionextendsRuntimeException{privateinterrorCode=0;......
  • C++ 模板类编译过程中出现“undefined reference to”问题
    问题描述C++在使用模板(template)类的时候,如果将类的成员函数的声明和实现分别放在.h头文件和.cpp源文件中,编译时会报错undefinedreferencexxx,找不到对应成员函数。起因.h文件中类的声明为://线程池,定义成模板类,为了代码的复用template<typenameT>classThreadPool{......
  • Qt报错:call to constructor of '_ConfigDaoImpl' is ambiguous
    Qt报错:calltoconstructorof'_ConfigDaoImpl'isambiguous原因configform.cpp:4:13:error:calltoconstructorof'_ConfigDaoImpl'isambiguousconfigdaoimpl.h:16:5:note:candidateconstructorconfigdaoimpl.h:17:5:note:candidateconst......
  • 关于VS2022使用EF生成实体模型报错的问题:运行转换:System.NullReferenceException:对象
    起因:之前版本vs2022生成EF模型一直没有问题,在更新了最新的vs2022之后,版本号17.6+,出现此问题:运行转换:System.NullReferenceException:对象引用未设置为对象的示例。在Microsoft.VisualStudio.TextTemplatingD21DB4521EFD493FAE41A9CE9DA80C875F3084552987498BD518713BDE91D14A......
  • Item 1: Consider static factory methods instead of constructors
    实际应用:packagejava.lang;publicfinalclassBooleanimplementsjava.io.Serializable,Comparable<Boolean>{publicstaticfinalBooleanTRUE=newBoolean(true);publicstaticfinalBooleanFALSE=newBoolean(false);....privatef......
  • 使用vue出现Uncaught TypeError: Vue is not a constructor错误
    原因是vue2和vue3写法不对正确是<!DOCTYPEhtml><html>   <head>      <metacharset="utf-8">      <title></title>      <scripttype="text/javascript"src="https://unpkg.com/vue@next"></s......
  • Android通过 SharedPreference 实现用户名与密码的存储与调用
    注:Android实验课(一)的内容一、实验原理1.1实验目标编程实现用户名与密码的存储与调用。1.2实验要求设计用户登录界面、登录成功界面、用户注册界面,用户注册时,将其用户名、密码保存到SharedPreference中,登录时输入用户名、密码,读取SharedPreference,读取不到该用户名提示用户不存在,用......
  • [linux]undefined reference to `__gxx_personality_v0'
    linux程序 #include#include#includeintcount=0;voidctrl_c_count(int);intmain(void){intc;void(*old_handler)(int);old_handler=signal(SIGINT,ctrl_c_count);while((c=getchar()!=''));printf("Ctrl_Ccount=%d",count);......
  • Template execution failed ReferenceError BASE_URL is not defined
    错误VueTemplateexecutionfailed:ReferenceError:BASE_URLisnotdefinedReferenceError:BASE_URLisnotdefined解决替换index.html替换前<linkrel="icon"href="<%=BASE_URL%>favicon.ico">替换后<linkrel="icon"......