Kat*_*tta 1 java boolean class actionlistener
我有一个布局和单个按钮的类.我想知道是否有内置的方法或功能,我可以检测功能中的布尔值是否已更改.我可以用一堆其他布尔值来做,但是正在寻找一种更优雅的方式.我认为它与"观察者"有关,但并不完全确定.
代码的简化版本如下:
Class checker{
boolean test1 = true;
boolean test2 = true;
checker(){
checkNow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//code to manage, and possibly change the value of the booleans test1 and test2.
//is there any built in function in java where i can test to see if the value of the booleans was changed in this actionListener function?
}
}});
}
Run Code Online (Sandbox Code Playgroud)
[是]有一个内置的方法或功能,我可以检测功能中的布尔值是否发生了变化?
您可以通过boolean使用setter 封装对变量的访问来实现:
private boolean test1 = true;
private boolean test2 = true;
private void setTest1(boolean newTest1) {
if (newTest1 != test1) {
// Do something
}
}
private void setTest2(boolean newTest2) {
if (newTest2 != test2) {
// Do something
}
}
Run Code Online (Sandbox Code Playgroud)
用电话代替这些变量的所有任务setTest1,并setTest2得到变化的可靠检测test1和test2.
| 归档时间: |
|
| 查看次数: |
2266 次 |
| 最近记录: |