a只能在这里决赛.为什么?如何a在onClick()不将其保留为私有成员的情况下重新分配方法?
private void f(Button b, final int a){
b.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
int b = a*5;
}
});
}
Run Code Online (Sandbox Code Playgroud)如何5 * a点击它返回?我的意思是,
private void f(Button b, final int a){
b.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
int b = a*5;
return b; // but return type is void
}
});
}
Run Code Online (Sandbox Code Playgroud)我遇到了一个关于C#的有趣问题.我有如下代码.
List<Func<int>> actions = new List<Func<int>>();
int variable = 0;
while (variable < 5)
{
actions.Add(() => variable * 2);
++ variable;
}
foreach (var act in actions)
{
Console.WriteLine(act.Invoke());
}
Run Code Online (Sandbox Code Playgroud)
我希望它输出0,2,4,6,8.但是,它实际输出5个10.
似乎是由于所有操作都涉及一个捕获的变量.结果,当它们被调用时,它们都具有相同的输出.
有没有办法解决这个限制,让每个动作实例都有自己的捕获变量?
final JTextField jtfContent = new JTextField();
btnOK.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent event){
jtfContent.setText("I am OK");
}
} );
Run Code Online (Sandbox Code Playgroud)
如果我省略final,我看到错误" 不能引用在不同方法中定义的内部类中的非最终变量jtfContent ".
为什么匿名内部类必须要求外部类实例变量为最终才能访问它?
我正在检查一些Java代码,我不擅长java至少有一些知识,如密封类,只读字段和不可覆盖的方法,但这对我来说很奇怪,final在方法中声明一个变量:
private static void queryGoogleBooks(JsonFactory jsonFactory, String query) throws Exception {
// Set up Books client.
final Books books = Books.builder(new NetHttpTransport(), jsonFactory)
.setApplicationName("Google-BooksSample/1.0")
.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
@Override
public void initialize(JsonHttpRequest request) {
BooksRequest booksRequest = (BooksRequest) request;
booksRequest.setKey(ClientCredentials.KEY);
}
})
.build();
Run Code Online (Sandbox Code Playgroud)
你能告诉我final在这种情况下的含义是什么吗?
这是完整的代码:
我需要更新一些UI并使用UI线程在UI线程内完成.runOnUiThread
现在UI的数据来自另一个Thread,由data此处表示.
如何将数据传递给Runnable,以便它们可用于更新UI?Android似乎不允许直接使用数据.有一种优雅的方式来做到这一点?
public void OnNewSensorData(Data data) {
runOnUiThread(new Runnable() {
public void run() {
//use data
}
});
}
Run Code Online (Sandbox Code Playgroud)
我的解决方案是private Data sensordata在runnable中创建一个fioeld ,并为其分配数据.如果原件Data data是最终的,这只有效.
public void OnNewSensorData(final Data data) {
runOnUiThread(new Runnable() {
private Data sensordata = data;
public void run() {
//use sensordata which is equal to data
}
});
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
Java Pass方法作为参数
是否可以在Java中将方法作为参数传递?如果我不能重复代码,那么下面的方法将是最好的行动方案.
public void enterUserInput(javax.swing.JTextField inputField, javax.swing.JTextField outputField, method rangeChecking){
String input;
double output;
input = inputField.getText();
output = Double.parseDouble(input);
if(rangeChecking(output)){
outputField.setText(input);
inputField.setText(null);
}
Run Code Online (Sandbox Code Playgroud)
我将从不同的类调用rangeChecking方法,每次调用enterUserInput时,rangeChecking方法都会有所不同
在方法内部定义的内部类不能访问方法的局部变量,除非标记了这些局部变量.我已经final查看了堆栈溢出和java代码牧场中的其他帖子,但它们似乎都没有完全回答关于如何标记的问题变量final允许内部类访问方法中的局部变量.
class MyOuter {
private String x = "Outer";
void fly(final int speed) {
final int e = 1;
class FlyingEquation {
public void seeOuter()
{
System.out.println("Outer x is " + x);
}
public void display()
{
System.out.println(e);// line 1
System.out.println(speed);// line 2
}
}
FlyingEquation f=new FlyingEquation();
f.seeOuter();
f.display();
}
public static void main(String args[])
{
MyOuter mo=new MyOuter();
mo.fly(5);
}
}
Run Code Online (Sandbox Code Playgroud)
我发现的解释:
局部变量存储在堆栈中,一旦方法调用完成,就会弹出堆栈并且局部变量不可访问,而 最终局部变量存储在内存的数据部分中,JVM即使在方法调用结束后也可能允许访问它们.在哪里data …
ratingS = new JSlider(1, 5, 3);
ratingS.setMajorTickSpacing(1);
ratingS.setPaintLabels(true);
int vote;
class SliderMoved implements ChangeListener {
public void stateChanged(ChangeEvent e) {
vote = ratingS.getValue();
}
}
ratingS.addChangeListener(new SliderMoved());
Run Code Online (Sandbox Code Playgroud)
如果我写上面的代码Eclipse告诉我这个:
不能在不同方法中定义的内部类中引用非最终变量vote
但如果我在int vote之前添加final,它会给我这个错误:
无法分配最终的局部变量投票,因为它是在封闭类型中定义的
那么,如何解决?
为什么我需要声明local variable的final,如果我的Inner class方法中定义需要使用它?
示例:
class MyOuter2 {
private String x = "Outer2";
void doStuff() {
final String y = "Hello World";
final class MyInner {
String z = y;
public void seeOuter() {
System.out.println("Outer x is "+x);
System.out.println("Local variable is "+y);
MyInner mi = new MyInner();
mi.seeOuter();
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
为什么String y需要是最终常量?它是如何影响的?
private void pushButtonActionPerformed(java.awt.event.ActionEvent evt)
{
final int c=0;
final JDialog d=new JDialog();
JLabel l=new JLabel("Enter the Element :");
JButton but1=new JButton("OK");
JButton but2=new JButton("Cancel");
final JTextField f=new JTextField(10);
JPanel panel = new JPanel();
but1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
c=Integer.parseInt(f.getText());
d.setVisible(false);
d.dispose( );
}
});
but2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
d.setVisible(false);
d.dispose( );
}
});
}
Run Code Online (Sandbox Code Playgroud)
我正在使用netbeans 7.1.1.这是我的代码,我已将'c'声明为"final int",但行"c = Integer.parseInt(f.getText());" 我收到错误"无法为最终变量赋值".如果我从声明中删除单词final并将其作为"int c",那么在同一行中我得到一个错误"无法从类中访问局部变量c;需要声明为final".谁能告诉我为什么会这样?