1

我在 Java 类中有一个属性,当 Java 类中的属性改变它的值时,我需要在同一个类中调用一个方法。我怎样才能做到这一点?

 boolean setInstallMode = false;

 public void callMeWhenChanged() {

    some actions . . . 

 }
4

4 回答 4

3

设为setInstallMode私有,然后仅通过 setter 方法进行设置。

private boolean installMode = false;

public void setInstallMode(boolean mode)
{
    installMode = mode;
    callMeWhenChanged();
}

public void callMeWhenChanged() {

   some actions . . . 

}
于 2013-11-13T16:18:16.777 回答
1

您可以将属性设为私有,然后为该属性设置一个 setAttribute 方法,然后在该 set 属性方法中调用 callMeWhenChanged

private boolean installMode

public void setInstallMode(boolean o){
    this.installMode = o;
    callMeWhenChanged()
    //WhateverElse
}
于 2013-11-13T16:18:44.100 回答
0
 private   boolean setInstallMode = false; // Make attribute as private field

 public void setInstallMode(boolean inBooleanValue){ // write public mutator for this attribute 
  this.setInstallMode =inBooleanValue; // update value

   callMeWhenChanged();// Call your logic
  }


 public void callMeWhenChanged() {

    some actions . . . 

 }
于 2013-11-13T16:18:56.483 回答
0

您可以像在此示例中那样使用属性更改侦听器:

http://examples.javacodegeeks.com/core-java/beans/bean-property-change-event-listener/

于 2013-11-13T16:19:01.200 回答