会记住其值的计数器

发布于 2024-08-26 04:34:02 字数 897 浏览 6 评论 0原文

我有一个对复数进行操作的任务。每个数字由双 r = 实部、双 i = 虚部和字符串名称组成。名称必须在构造函数中设置,因此我创建了 int 计数器,然后将其值发送到 setNextName 函数并获取名称字母。不幸的是,递增这个“计数器”值只能在构造函数内起作用,然后它再次设置为 0。如何处理?一些常量值?第二个问题是我还需要提供 setNextNames(char c) 函数来更改计数器当前值。

代码:

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
//
}

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
//
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风渺 2024-09-02 04:34:02

很难说你在做什么,但我怀疑这会解决你眼前的问题:

private static int counter = 0;

It's hard to tell what you're doing, but I suspect this will solve your immediate problem:

private static int counter = 0;
合久必婚 2024-09-02 04:34:02

您应该将 counter 设为静态。

您还应该将 nameTab 设为私有静态字段,然后在 setNextName() 中,您可以迭代它以查找与给定字符对应的名称,并获取其索引。 (在普通的 ASCII 世界中,当然可以通过从给定字符中减去“A”的数值来简单地计算索引,但我不太确定它如何在 Java、Unicode 和疯狂的输入中计算出来 -迭代是安全的。)

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.)

喜你已久 2024-09-02 04:34:02

在面向对象语言中,类中通常有两种类型的变量:

  • 每个实例类唯一的实例变量
  • 类的所有实例共享的变量

给定一个类:

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

    // instance variable
    private String name;

    // other code goes here
}

如果您要执行类似的操作:

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

然后做类似的事情:

a.setName("Jane Foe");

人“a”的名字会改变,但人“b”的名字将保持不变。

如果您一天早上醒来并决定想要 3 只眼睛:

Person.setNumberOfEyes(3);

那么人“a”和人“b”以及其他所有 Person 实例也会突然拥有 3 只眼睛。

您想在计数器声明中添加“静态”。

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.

Hello爱情风 2024-09-02 04:34:02

您的代码是否被多个线程使用,我建议将计数器设置为静态并不能解决您的问题。

您需要格外小心,使用 lock 关键字实现线程同步,如下所示。

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++; 
    }
}

这将在增加计数器的同时确保线程安全(还有另一种方法可以提供线程安全性,但这种方法的代码最少)。

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).

海的爱人是光 2024-09-02 04:34:02

由于字段 counter 不是静态,因此每个对象都有自己的计数器。

Because the field counter is not static, every object has its own counter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文