2个类之间的通信

时间:2013-04-19 15:07:27

标签: java

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

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

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.我怎样才能传递变量的实际值。

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

8 个答案:

答案 0 :(得分:2)

A a = new A();

实例化之后(上面的语句)你需要调用这个会增加计数器的方法。

示例:

   a.incrementCounter();

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

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

答案 1 :(得分:1)

  

为了举例说明,此时变量的值为1。

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

您使用的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());
    }
}

答案 2 :(得分:0)

使变量静态,以便它与类相关联。

public class A{
   private static int counter = 0;

   public int getCounter(){
      counter++;
      return counter;
   }

答案 3 :(得分: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;
        }


}

答案 4 :(得分:0)

使用constructors / setter ......

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());
    }
}

答案 5 :(得分: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;

答案 6 :(得分: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

答案 7 :(得分: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
    }
}