java - 有关编程的菜鸟问题
阿神
阿神 2017-04-17 11:28:33
[Java讨论组]

先上代码

convertView.setOnClickListener(new OnClickListener() {
    long lastClick;
    @Override
    public void onClick(View v) {
        if (System.currentTimeMillis() - lastClick <= 1000) {
        return;
        }
    lastClick = System.currentTimeMillis(); 
    }
});

这样写是编译通过的,但是如果我把long lastClick;放到onClick的里面第一行,然后再进行if来判断,就会报错要求对lastClick进行初始化,这时为什么啊?求解

阿神
阿神

闭关修行中......

全部回复(1)
PHPz

long lastClick放在 函数外, 是对象的成员变量, 默认初始化为0.

放在函数内, 称为本地变量. JLS7, #16 里有写:

"EACH local variable (§14.4) and every blank final field (§4.12.4,
§8.3.1.2) must have a definitely assigned value when any access of its
value occurs. An access to its value consists of the simple name of
the variable (or, for a field, the simple name of the field qualified
by this) occurring anywhere in an expression except as the left-hand
operand of the simple assignment operator = (§15.26.1). For every
access of a local variable or blank final field x, x must be
definitely assigned before the access, or a compile-time error occurs
"

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号