请修复它不兼容的类型,需要在timer.schedule run()curInterval上找到布尔值int,我的代码有什么问题?

 public class HeartbeatPacket implements HeartbeatStop {
        private final String TAG = getClass().getSimpleName();
        private int curInterval = 0;
        private HeartbeatStop heartbeatStop = null;
        private final int setInterval;
        private Timer timer;

        public HeartbeatPacket(HeartbeatStop heartbeatStop, int setInterval) {
            this.heartbeatStop = heartbeatStop;
            this.curInterval = setInterval;
            this.setInterval = this.curInterval;
        }

        public void callStopFun() {
            if (this.heartbeatStop != null) {
                this.heartbeatStop.callStopFun();
            }
        }

        public void recover() {
            synchronized (this) {
                this.curInterval = this.setInterval;
            }
        }

        private void run() {
            if (this.timer == null) {
                Log.e(this.TAG, "null == timer");
            } else {
                this.timer.schedule(new TimerTask() {
                    public void run() {
                        synchronized (this) {
    //this is the problem section
                            if (HeartbeatPacket.this.curInterval = HeartbeatPacket.this.curInterval - 1 < 0) {
                                HeartbeatPacket.this.callStopFun();
                                HeartbeatPacket.this.recover();
                                HeartbeatPacket.this.stop();
                            }
                        }
                    }
                }, 0, 1000);
            }
        }

        public void start() {
            recover();
            this.timer = new Timer();
            run();
        }

        public void stop() {
            this.timer.cancel();
            this.timer = null;
        }``
    }


我认为Java编译器应该抱怨
if(HeartbeatPacket.this.curInterval = HeartbeatPacket.this.curInterval-1

最佳答案

运算符的优先顺序不是您想的那样,尤其是对于=<。首先进行比较,得到boolean类型,然后将其分配给int字段-这是非法的。

通常,在if条件下结合变量的赋值和/或修改不是一个好主意。很难阅读,而且容易出错(如此处所示)。在if之前更改您的值,然后将其与纯值进行比较。

关于java - 不兼容的类型-找到:int必需:在timer.schedule上的 boolean 值run()curInterval,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56713967/

10-09 15:58