java 使用 getter 和 setter 方法并返回 0
java using getter and setter methods and returning 0
我在 2 个单独的 classes 中创建了 2 个计时器。一个计时器递增 int 计数器。另一个使用get方法并打印出int counter的值。
问题是如果我使用 private int counter
,第二个计时器只打印出 0、0、0 等
而如果我使用 private static counter
它会打印出 1,2,3,4,5 等,这正是我想要的。但我宁愿不使用 static
因为有人告诉我它是不好的做法。
这是我的主要内容 class:
import java.util.Timer;
public class Gettest {
public static void main(String[] args) {
classB b = new classB();
classC c = new classC();
timer = new Timer();
timer.schedule(b, 0, 2000);
Timer timer2 = new Timer();
timer2.schedule(c, 0, 2000); }}
class B 带定时器 1
import java.util.TimerTask;
public class classB extends TimerTask {
private int counter = 0;
public int getint()
{ return counter;}
public void setint(int Counter)
{ this.counter = Counter;}
public void run()
{ counter++;
this.setint(counter);}}
class C 带定时器 2
import java.util.TimerTask;
public class classC extends TimerTask
{
classB b = new classB();
public void run(){
System.out.println(b.getint());}}
我该如何修复才能使用 private int counter;
?
您有两个完整的 unique/separate ClassB 实例,一个 运行 带有计时器,另一个显示。显示的那个永远不会改变,因为它不是定时器中的 运行,所以它总是显示初始默认值 0。
如果你改变它,那么你只有一个实例:
import java.util.Timer;
import java.util.TimerTask;
public class Gettest {
private static Timer timer;
public static void main(String[] args) {
ClassB b = new ClassB();
ClassC c = new ClassC(b); // pass the B instance "b" into C
timer = new Timer();
timer.schedule(b, 0, 2000);
Timer timer2 = new Timer();
timer2.schedule(c, 0, 2000);
}
}
class ClassB extends TimerTask {
private int counter = 0;
public int getint() {
return counter;
}
public void setint(int Counter) {
this.counter = Counter;
}
public void run() {
counter++;
this.setint(counter);
}
}
class ClassC extends TimerTask {
ClassB b;
// add a constructor to allow passage of B into our class
public ClassC(ClassB b) {
this.b = b; // set our field
}
public void run() {
System.out.println(b.getint());
}
}
代码将起作用。
作为附带建议,再次请处理您的代码格式,并努力使其符合 Java 标准。例如,请看我上面的代码。
你基本上已经创建了两个左右独立的实例,在内存中调用了两个不同的对象。那么,一个对象的实例如何打印另一个对象的值。使用静态计数器或将引用传递给同一对象。
我在 2 个单独的 classes 中创建了 2 个计时器。一个计时器递增 int 计数器。另一个使用get方法并打印出int counter的值。
问题是如果我使用 private int counter
,第二个计时器只打印出 0、0、0 等
而如果我使用 private static counter
它会打印出 1,2,3,4,5 等,这正是我想要的。但我宁愿不使用 static
因为有人告诉我它是不好的做法。
这是我的主要内容 class:
import java.util.Timer;
public class Gettest {
public static void main(String[] args) {
classB b = new classB();
classC c = new classC();
timer = new Timer();
timer.schedule(b, 0, 2000);
Timer timer2 = new Timer();
timer2.schedule(c, 0, 2000); }}
class B 带定时器 1
import java.util.TimerTask;
public class classB extends TimerTask {
private int counter = 0;
public int getint()
{ return counter;}
public void setint(int Counter)
{ this.counter = Counter;}
public void run()
{ counter++;
this.setint(counter);}}
class C 带定时器 2
import java.util.TimerTask;
public class classC extends TimerTask
{
classB b = new classB();
public void run(){
System.out.println(b.getint());}}
我该如何修复才能使用 private int counter;
?
您有两个完整的 unique/separate ClassB 实例,一个 运行 带有计时器,另一个显示。显示的那个永远不会改变,因为它不是定时器中的 运行,所以它总是显示初始默认值 0。
如果你改变它,那么你只有一个实例:
import java.util.Timer;
import java.util.TimerTask;
public class Gettest {
private static Timer timer;
public static void main(String[] args) {
ClassB b = new ClassB();
ClassC c = new ClassC(b); // pass the B instance "b" into C
timer = new Timer();
timer.schedule(b, 0, 2000);
Timer timer2 = new Timer();
timer2.schedule(c, 0, 2000);
}
}
class ClassB extends TimerTask {
private int counter = 0;
public int getint() {
return counter;
}
public void setint(int Counter) {
this.counter = Counter;
}
public void run() {
counter++;
this.setint(counter);
}
}
class ClassC extends TimerTask {
ClassB b;
// add a constructor to allow passage of B into our class
public ClassC(ClassB b) {
this.b = b; // set our field
}
public void run() {
System.out.println(b.getint());
}
}
代码将起作用。
作为附带建议,再次请处理您的代码格式,并努力使其符合 Java 标准。例如,请看我上面的代码。
你基本上已经创建了两个左右独立的实例,在内存中调用了两个不同的对象。那么,一个对象的实例如何打印另一个对象的值。使用静态计数器或将引用传递给同一对象。