1

我是一个新手java程序员,我正在尝试制作我的第一个项目。

我需要在 2 个类之间传递一个变量,这很好。问题是变量有一个变化的值,我不能传递实际值。这是一个例子:

public class A{
    private int counter = 0;

    public int getCounter(){
        return counter;
    }

    //here some code which will increase or decrease the value of the counter variable 
    //lets say for the sake of the example that at this point the value of the variable is 1.
    //counter = 1;
}

public class B{
    public static void main(String[] args) {
        A a = new A();
        System.out.println(a.getCounter());// here I need the actual counter variable value which is currently: 1
    }
}

我的问题是我总是收到 0。如何传递变量的实际值。

非常感谢任何帮助或建议。

4

8 回答 8

2
A a = new A();

在实例化(上面的语句)之后,您需要调用将在此处增加计数器的方法。

例子:

   a.incrementCounter();

然后下面的语句将获得计数器值。

 System.out.println(a.getCounter());
于 2013-04-19T15:09:00.243 回答
1

为了这个例子,假设此时变量的值为 1。

不,在读取代码时,值没有改变。您在class-block 中所做的只是定义一个类,即对象的“模板”。那时,没有设置任何值。

您使用的a.getCounter()已经完成了正确的工作:它返回计数器变量的当前值。a如果它没有返回 1,那么显然这个值还没有改变。

public class A {
    private int counter = 0;
    public int getCounter() {
        return counter;
    }
    public void increaseCounter() {
        counter++;
    }
}

public class B {
    public static void main() {
        A a = new A();
        System.out.println(a.getCounter());
        a.increaseCounter();
        System.out.println(a.getCounter());
    }
}
于 2013-04-19T15:13:45.023 回答
0

您可以从构造函数和/或创建更改值的方法中执行此操作。

public class A
{
    private int counter = 0;

    public A()
    {
        // value is set first time you create an instance of A. (e.g when you do A a = new A();
        counter = 1;
    }

    public int getCounter()
    {
        return counter;
    }

    public void incrementCounter()
    {
        counter++;
    }

}

public class B
{
    public static void main(String[] args) 
    {
        A a = new A();
        System.out.println(a.getCounter());// Output : 1
        a.incrementCounter();
        System.out.println(a.getCounter());// Output : 2

        a.incrementCounter();
        a.incrementCounter();
        a.incrementCounter();
        System.out.println(a.getCounter());// Output : 5
    }
}
于 2013-04-19T16:11:54.560 回答
0
   public static void main(String[] args) {
        A a = new A();
        a.setCounter(5);
        System.out.println(a.getCounter()); 
    }


public class A{
    private int counter = 0;

    public int getCounter(){
        return counter;
    }



      public void setCounter(int count ){
            this.counter=count;
        }


}
于 2013-04-19T15:11:39.267 回答
0

使用构造函数/设置器...

public class A{
    private int counter = 0;

    public A(int c){
        counter = c
    }

    public int getCounter(){
        return counter;
    }

    public void setCounter(int c){
        counter = c;
    }

    public void incCounter(){
        counter++;
    }
}

public class B{
    public static void main(String[] args) {
        A a = new A(123);
        System.out.println(a.getCounter());

        a.setCounter(456);
        System.out.println(a.getCounter());

        a.incCounter();
        System.out.println(a.getCounter());
    }
}
于 2013-04-19T15:12:02.640 回答
0
class A {

    private int counter = 0;

    public int getCounter() {
        return counter;
    }

    public int increment() {//////////create increment Method which will increase the counter , or do any function you want
        return counter++;
    }

    public void  setCounter(int c) {///////////this method will allow you to set the counter
        counter=c;
     }
  }

class B {

    public static void main(String[] args) {
        A a = new A();
        a.increment();///////if you call this function will change your counter , if not , you will get it = 0
        System.out.println(a.getCounter());
    }
}

A a = new A();
System.out.println(a.getCounter());

输出= 0

A a = new A();
a.increment();
System.out.println(a.getCounter());

输出=1

 a = new A();
 a.setCounter(10);//////////here you set the `counter`  by 10
 System.out.println(a.getCounter());

输出=10;

于 2013-04-19T15:12:07.823 回答
0

您有一个类 (Counter) 来管理 counter int 变量。

您希望一个或多个其他类能够增加和/或获取计数器值。

在这种情况下,这些类的每个实例都应该引用相同的 Counter 实例(存储为成员变量,传递给它们的构造函数或 setter 方法)。

class Counter {
    private int counter = 0;
    public int getValue() { return counter; }
    public void increment() { counter++; }
    public String toString() { return Integer.toString(counter); }
}

class CounterUser {
    private final Counter counter;
    public CounterUser(Counter counter) { this.counter = counter; }
    public String toString() { return Integer.toString(counter.getValue()); }
}

class Test {
    public static void main(String[] args) throws Exception {
        Counter counter = new Counter();
        CounterUser a = new CounterUser(counter);
        CounterUser b = new CounterUser(counter);
        System.out.printf("%s %s %s\n", counter, a, b);
        counter.increment();
        System.out.printf("%s %s %s\n", counter, a, b);
        b.increment();
        System.out.printf("%s %s %s\n", counter, a, b);     }
}

输出:

0 0 0
1 1 1
2 2 2
于 2013-04-19T15:31:01.213 回答
0

将变量设为静态,以便将其与类关联。

public class A{
   private static int counter = 0;

   public int getCounter(){
      counter++;
      return counter;
   }
于 2013-04-19T15:08:28.413 回答