Actually, I want to create counter objects, all of them have to do, they count as numbers. And in my reset method, I want to reset the value of each object. It's probably very easy, but I'm a new one.
public square counter {random number = new random (); Counter () {random number = new random (); } Public Random GetNumber () {Return Number; } Public Zero Satumumber (Random Number) {this.number = number; } Public static zero main (string [] args) {counter counter 1 = new counter (); Counter counter 2 = new counter (); Counter counter 3 = new counter (); Counter counter 4 = new counter (); Counter counter 5 = new counter (); } Public Definition Zero Resetector () {}}
first option: remember each example Keep Counter . Collect each example of the counter in some static archives. To reset all, just repeat all the items in the collection.
Use of weak contexts to avoid this issue should be avoided that counter objects are in existence indefinitely, due to their reference from within the stable collection, objects that are only weak contexts Are referred to, eventually collected by the garbage collector. The collection of each instance can be obtained by declaring the controller private code> and only static The member allows creation only through the function which will also register. (Or use some other incarnation of the pattern.) I believe that a factory is the way to go here, because every creation of an object has to do a side effect too. But perhaps it will do with the static code to get the counter constructor register this . second option: Generation Counter
Keep a stable generation counter type long , and in each counter Also keep a copy of this counter. When resetting all the counters, simply raise the Static generation counter to getNumber () method then static against the copy of the generation counter Reset tax and counter if the stable generation counter has been changed. / P> (I really do not know the name "official" for this trick.)
Comments
Post a Comment