会记住其值的计数器
我有一个对复数进行操作的任务。每个数字由双 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
很难说你在做什么,但我怀疑这会解决你眼前的问题:
It's hard to tell what you're doing, but I suspect this will solve your immediate problem:
您应该将
counter
设为静态。您还应该将
nameTab
设为私有静态字段,然后在setNextName()
中,您可以迭代它以查找与给定字符对应的名称,并获取其索引。 (在普通的 ASCII 世界中,当然可以通过从给定字符中减去“A”的数值来简单地计算索引,但我不太确定它如何在 Java、Unicode 和疯狂的输入中计算出来 -迭代是安全的。)You should make
counter
static.You should also make
nameTab
a private static field, then insetNextName()
, 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.)在面向对象语言中,类中通常有两种类型的变量:
给定一个类:
如果您要执行类似的操作:
然后做类似的事情:
人“a”的名字会改变,但人“b”的名字将保持不变。
如果您一天早上醒来并决定想要 3 只眼睛:
那么人“a”和人“b”以及其他所有 Person 实例也会突然拥有 3 只眼睛。
您想在计数器声明中添加“静态”。
In OO languages there are typically two types of variables that go into a class:
Given a class like:
If you were to do something like:
and then do something like:
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:
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.
您的代码是否被多个线程使用,我建议将计数器设置为静态并不能解决您的问题。
您需要格外小心,使用 lock 关键字实现线程同步,如下所示。
这将在增加计数器的同时确保线程安全(还有另一种方法可以提供线程安全性,但这种方法的代码最少)。
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.
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
不是静态
,因此每个对象都有自己的计数器。Because the field
counter
is notstatic
, every object has its own counter.