Friday, 15 June 2012

java - Increment counter as object is created not working -


I just want to raise a variable, numOfBodies am, as has created a new body and use this value my main Is in the class. Why is not it working properly? Although I used to work it steadily?

  Int DeltaTime = 500 * body.DateName ();  

  public class physical {public static inte numOfBodies; Public body () {numOfBodies ++; } Public static int getNum () {return numOfBodies; }}  

On the basis of this note:

I do not think it was relevant to the question. I am pressed once after the program is started by pressing 'M'. I want Deltataim to update because I 'm' and press more often, namely to create more bodies

It seems that my guess is right, you believe That DeltaTime has created a new body automatically, and it is not how Java works. To do this, you must update the Delta Time clearly. One way is to use a supervisor design pattern, perhaps whenever a new body is created, using the PropertyChangeSupport to update the deltaTime.

Although it is being said, I never used to listen to a stable property without changing a property

But for example:

  import java.beans.PropertyChangeEvent; Import java.beans.PropertyChangeListener; Import java.beans.PropertyChangeSupport; Import java.util.Scanner; Public Class TestBody {Private Stable Last String QUIT = "left"; Public static int deltaTime = 0; public static void (String [] args) {Body.addPropertyChangeListener (Body.NUM_OF_BODIES, new PropertyChangeListener () {@Override public void propertyChange (PropertyChangeEvent evt) {deltaTime = 500 * Body.getNum (); println ( "Deltataim:" + Delta time);}}); Scanner scan = new scanner (System.in); String line = ""; While (! Line.contains (QUIT)) {System.out.print ("Please enter to create a new body, or type \" Skip \ "to leave:"); Line = scan. NXtine (); Body of body = new body (); }}} Class physical {public static final string NUM_OF_BODIES = "number of bodies"; Private Static PropertyChangeSupport pcSupport = New PropertyChangeSupport (Body.class); Private static volatile int numOfBodies; Public body () {int oldValue = numOfBodies; NumOfBodies ++; Int newValue = numOfBodies; PcSupport.firePropertyChange (NUM_OF_BODIES, old fashion, newcomer); } Public static int getNum () {return numOfBodies; } Public static Zero addPropertyChangeListener (PropertyChangeListener) {pcSupport.addPropertyChangeListener (l); } Public static zero extraction Change the ChangeElaster (Property Change Listener) {pcSupport.removePropertyChangeListener (l); } Public static void addPropertyChangeListener (string property name, property change Listnr l) {pcSupport.addPropertyChangeListener (propertyName, l); } Public Static Zero Extracting ReplaceLearer (String Properties, Property Change Listener) {pcSupport.removePropertyChangeListener (propertyName, l); }}  

No comments:

Post a Comment