不兼容的类型 - 找到:int required:boolean on timer.schedule run() cur



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

 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 <0( .您是否碰巧看到一些编译错误消息?

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

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

最新更新