How can I reset all values ​​of an object with just one method call?

Basically, I want to create Counter objects, all they need to do is hold numerical values. And in my resetCounters method, I would like to reset the values ​​of each object. This is probably very simple, but I'm new.

public class Counter
{
    Random number = new Random();

    Counter()
    {
        Random number = new Random();
    }

    public Random getNumber()
    {
        return number;
    }

    public void setNumber(Random number)
    {
        this.number = number;
    }

    public static void main(String[] args) 
    {
        Counter counter1 = new Counter();
        Counter counter2 = new Counter();
        Counter counter3 = new Counter();
        Counter counter4 = new Counter();
        Counter counter5 = new Counter();

    }

    public static void resetCounters()
    {

    }
    }
+5
source share
5 answers

First option: Remember each instance Counter.

Collect each instance Counterin some static collection. To reset everything, just iterate over all the elements of the collection. But strong links are too strong for this - make sure this is a collection of weak links .

Notes:

  • , , Counter - . , , .
  • Counter private - static, . ( Factory.) , factory - , . , , , Counter this .

:

static long, . static. getNumber() static reset , static .

( "" . O (1)?)

+5

, , , , , , ...

1: "counter1, counter2, counter3", ( ).

   public class Counter {
       static Counter counter1 = new Counter();
       ...
       public void resetCounters() {
         counter1.clear();
         counter2.clear();
          ...
        }
      }

2: , , , , :

public class Counter {
  public static void main(String[] args) {
    Counter[] counters = {new Counter(), new Counter(), new Counter(), new Counter(), new Counter()};
    ...
  }
  static void resetCounters(Counter[] counters) {
    for (Counter c : counters) {
      c.reset();
    }
  }
}

, , factory. , , , .

+4

, - , ArrayList.

List<Counter> counters = new ArrayList<Counter>();

, .add(). resetCounters() :

public static void resetCounters(List<Counter> counters) {
    for(Counter c: counters) {
        // perform some action to reset the counters, as described by you
    }
}
+2

, , - -, , factory .

public class CounterFactory{
      private List<Counter> counters = new ArrayList<Counter>();

      public Counter createCounter(){
          Counter c = new Counter();
          counters.add(c);
          return c;
      }
      public void resetCounters(){
          for(Counter c : counters) c.setNumber(new Random());
      }
}

:

public static void main(String[] args) 
{
    CounterFactory f = new CounterFactory();
    Counter counter1 = f.createCounter();
    Counter counter2 = f.createCounter();
    Counter counter3 = f.createCounter();
    Counter counter4 = f.createCounter();
    Counter counter5 = f.createCounter();

    // Reset all counters
    f.resetCounters();
}
+1

1. , nos , Varaible, .

:

Random number;

2. ArrayList Counter.

ArrayList<Counter> arr = new ArrayList<Counter>();

3. - ArrayList.

4. reset . , .

5. reset...

for (Counter c : arr){

      c.reset();
    }

6. In reset (), do the following.

public void reset(){

         this.number = 0;

     }
+1
source