Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 两个班级之间的交流_Java - Fatal编程技术网

Java 两个班级之间的交流

Java 两个班级之间的交流,java,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

我是一名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。如何传递变量的实际值


非常感谢您提供的任何帮助或建议。

将变量设置为静态,以便它与类关联

public class A{
   private static int counter = 0;

   public int getCounter(){
      counter++;
      return counter;
   }
在实例化(上面的语句)之后,您需要调用将在此处递增计数器的方法

例如:

   a.incrementCounter();
然后下面的语句将得到计数器值

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

使用构造函数/设置器

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

输出=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

为了便于示例,我们假设此时变量的值为1

不,在读取代码时,该值没有更改。在
-块中所做的一切就是定义一个类,即对象的“模板”。但当时没有设置任何值

您使用的
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());
    }
}
您有一个类(Counter)来管理Counter int变量

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

 System.out.println(a.getCounter());
在这种情况下,这些类的每个实例都应该有一个对计数器的相同实例的引用(存储为成员变量,传递给它们的构造函数或setter方法)

输出:

0 0 0
1 1 1
2 2 2

您可以通过构造函数和/或创建更改值的方法来执行此操作

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

您可以将值传递给另一个类的构造函数。您应该显示完整的代码,包括更改计数器的部分。你发布的内容将给出你得到的确切结果。
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());
    }
}
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
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
    }
}