简体   繁体   中英

Counter that will remember its value

I have a task to operate on complex number. Each number consists of double r = real part, double i = imaginary part and String name. Name must be set within constructor, so I've created int counter, then I'm sending its value to setNextName function and get name letter back. Unfortunately incrementing this 'counter' value works only within costructor and then it is once again set to 0. How to deal with that?Some constant value? And second problem is that I also need to provide setNextNames(char c) function that will change the counter current value.

The code :

public class Imaginary {

private double re;
private double im;
private String real;
private String imaginary;
private String name;
private int counter=0;

public Imaginary(double r, double u){
    re = r;
    im = u;
    name = this.setNextName(counter);
    counter++;
}

public static String setNextName(int c){

    String nameTab[] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N",
                        "O","P","Q","R","S","T","U","W","V","X","Y","Z"};

    String setName = nameTab[c];
    System.out.println("c: "+c);
    return setName;
}

public static String setNextName(char c){

//
//don't know how to deal with this part
//
}

很难说出自己在做什么,但是我怀疑这将解决您的直接问题:

private static int counter = 0;

You should make counter static.

You should also make nameTab a private static field, then in setNextName() , you can iterate through it to find the name corresponding to the given character, and get its index. (in the plain ASCII world, of course one could simply calculate the index by subtracting the numeric value of 'A' from the given character, but I am not quite sure how it would work out with Java, in Unicode, with crazy inputs - iteration is on the safe side.)

In OO languages there are typically two types of variables that go into a class:

  • instance variables that are unique to each instance
  • class variables that are shared by all instances of the class

Given a class like:

public class Person
{
    // class variable
    private static int numberOfEyes;

    // instance variable
    private String name;

    // other code goes here
}

If you were to do something like:

Person a = new Person("Jane Doe");
Person b = new Person("John Doe");

and then do something like:

a.setName("Jane Foe");

the name for Person "a" would change, but the one for Person "b" would stay the same.

If you woke up one morning and decided you wanted 3 eyes:

Person.setNumberOfEyes(3);

then Person "a" and Person "b" and every other Person instance out there would suddenly have 3 eyes as well.

You want to put "static" in your counter declaration.

is your code being used by multiple threads than i would suggest that making counter static won't solve ur problem.

you need to take extra care by implementing thread synchronization use lock keyword as shown below.

private static readonly obj = new Object();
private static int counter =0;

public Imaginary(double r, double u)
{ 
    re = r; 
    im = u; 
    lock(obj)
    {
        name = this.setNextName(counter); 
        counter++; 
    }
}

this will ensure thread safety also while incrementing your counter (there are another ways also to provide thread security but this one is having least code).

因为字段counter不是static ,所以每个对象都有自己的计数器。

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