合成访问器方法警告 [英] Synthetic accessor method warning
问题描述
我在eclipse中做了一些新的警告设置。有了这些新设置,我面临一个奇怪的警告。阅读后我知道它是什么但找不到删除它的方法。
I've made some new warning settings in eclipse. With these new settings I'm facing a strange warning. After reading I got to know what it is but couldn't find a way to remove it.
这是我的示例代码问题
public class Test {
private String testString;
public void performAction() {
new Thread( new Runnable() {
@Override
public void run() {
testString = "initialize"; // **
}
});
}
}
* 的行*在eclipse中给我一个警告
The line with ** gives me a warning in eclipse
Read access to enclosing field Test.testString is emulated by a synthetic accessor method.
Increasing its visibility will improve your performance.
问题是,我不想更改 testString的访问修饰符
。
另外,不想为它创建一个getter。
Problem is, I don't want to change the access modifier of testString
.
Also, don't want to create a getter for it.
应该做些什么改变?
More descriptive example
public class Synthetic
{
private JButton testButton;
public Synthetic()
{
testButton = new JButton("Run");
testButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
/* Sample code */
if( testButton.getText().equals("Pause") ) {
resetButton(); // **
} else if( testButton.getText().equals("Run") ) {
testButton.setText("Pause"); // **
}
}
}
);
}
public void reset() {
//Some operations
resetButton();
}
private void resetButton() {
testButton.setText("Run");
}
}
行数 **
给了我同样的警告。
Lines with **
gives me the same warning.
推荐答案
在你的第二个例子中,没有必要访问 testButton
直接;您可以通过检索动作事件的来源来访问它。
In your second example it is not necessary to access testButton
directly; you can access it by retrieving the source of the action event.
对于 resetButton()
方法,您可以添加如果您已经这样做,那么传递该对象以进行操作的参数就会降低其访问限制。
For the resetButton()
method you can add an argument to pass the object to act upon, if you've done that it is not such a big problem lowering its access restrictions.
这篇关于合成访问器方法警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!