简体   繁体   中英

Does Java have property observers similar to Swift's willSet & didSet methods?

I want to observe whether a variable's value has been changed. I was wondering if Java has something that is equivalent to the willSet and didSet methods in Swift ?

Not as a language. Java on its own does not really know properties of any kind. What you can do, though:

  1. If the object is yours, just change your setter. That's why they're in the first place.
  2. If that is not good enough for you and you want something better, at least I hope you have Java 8. In there, you can use JavaFX's properties: eg the LongProperty .
  3. If you can't change the code, you'll have to go for AOP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM