在下面的代码中,如果我从EditText中删除了关键字final,我在将EditText对象(et)传递给intent的第(6)行中出现错误...我必须在这里知道final关键字的意义……
final EditText et=(EditText)findViewById(R.id.t);
Button b=(Button)findViewById(R.id.b1);
b.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)<br>
{
Intent on=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+et.getText()));
startActivity(on);
}
});发布于 2010-12-30 02:05:37
这是因为您在这里使用了闭包。这意味着内部类使用内界类的上下文。要使用它,变量应该被声明为final,这样才不会被更改。
请参阅更多here。
发布于 2010-12-30 02:07:08
Final本质上意味着变量et在任何时候都不会被重新赋值,并且会一直存在。这意味着内部类,就像你的监听器一样,可以相信它不会被其他线程重新分配,这可能会导致各种麻烦。
final还可以用来修改方法或类定义,这意味着方法不能被子类覆盖,或者类不能被扩展。
发布于 2015-01-14 15:26:50
JAVA中“final”关键字的用途可以定义为三个层次:类、方法、变量
Java final variable: If you make any variable as final, you cannot change the value of final variable (It will be constant).
Java final method: If you make any method as final, you cannot override it.
Java final class: If you make any class as final, you cannot extend it.https://stackoverflow.com/questions/4556503
复制相似问题