Java:使用在先前方法中构造的对象

时间:2014-07-15 22:01:10

标签: java oop

我正在创建一个程序,其中包含一个带有某些相关字段和方法的Count对象。

在程序中,Count对象在setupCounter()方法中初始化,而其方法实际上在counterControl()方法中使用。

但是,当我尝试调用之前在setupCounter()方法中初始化的Count实例时,我被告知" count无法解析",所以我假设只创建了Count的实例有本地范围。

有谁知道如何克服Counter实例范围不足的问题?以下是我的代码:

import java.util.Scanner;

public class Main {

  static Scanner in = new Scanner(System.in);

  public static void main(String[] args) {
    setupCounter();
    for (;;) {
      counterControl();
    }

  }

  public static void setupCounter() {
    int startVal;
    int incrementVal;

    System.out.println("Please enter the starting value of the counter:");
    startVal = in.nextInt();
    System.out.println("Please enter the increment/decrement amount on the counter:");
    incrementVal = in.nextInt();

    Count count = new Count(startVal, incrementVal);
  }

  public static void counterControl() {
    String control;

    System.out.println("The following controls can be applied to the counter:");
    System.out.println("Increment - i");
    System.out.println("Decrement - d");
    System.out.println("Exit - x");

    control = in.nextLine();

    switch (control) {
      case "i":
        count.increment();
        break;
      case "d":
        count.decrement();
        break;
      case "x":
        System.exit(0);
        break;
    }
  }
}

谢谢。

4 个答案:

答案 0 :(得分:0)

public static void main(String[] args) {
    Count counter = setupCounter();
    //now you can refer to counter

....

  public static Count setupCounter() {
    int startVal;
    int incrementVal;

    System.out.println("Please enter the starting value of the counter:");
    startVal = in.nextInt();
    System.out.println("Please enter the increment/decrement amount on the counter:");
    incrementVal = in.nextInt();

    return new Count(startVal, incrementVal);
  }

答案 1 :(得分:0)

你的功能没有返回任何东西。您必须在函数末尾使用“return”关键字才能将其传递。

如果它是一个无效函数(就像你的那个),它将不会返回/传递任何东西,因此单词“void”。

编辑:您可能也知道您正在将该功能用作静态功能。请进一步了解静态函数及其用途,以便更好地理解。

编辑2:如果要返回“Count”类型的数据,则应使用“Count”替换void和static

答案 2 :(得分:0)

您的代码中有各种破碎的东西。我为你创造了我认为你缺少的伯爵课程。并修改了你的方法......

import java.util.Scanner;

public class Main2 {

    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        Count count = setupCounter();
        for (;;) {
            counterControl(count);
        }

    }

    public static Count setupCounter() {
        int startVal;
        int incrementVal;

        System.out.println("Please enter the starting value of the counter:");
        startVal = in.nextInt();
        System.out.println("Please enter the increment/decrement amount on the counter:");
        incrementVal = in.nextInt();

        return new Count(startVal, incrementVal);
    }

    public static void counterControl(Count count) {
        String control;

        System.out.println("The following controls can be applied to the counter:");
        System.out.println("Increment - i");
        System.out.println("Decrement - d");
        System.out.println("Exit - x");

        control = in.nextLine();
        System.out.println(count.getVal());

        switch (control) {
            case "i":
                count.increment();
                break;
            case "d":
                count.decrement();
                break;
            case "x":
                System.exit(0);
                break;
        }
    }

    private static class Count {

    private int startVal, incrementVal;
    private Count(int startVal, int incrementVal) {
        this.startVal = startVal;
        this.incrementVal = incrementVal;
    }

    private void increment() {
        this.startVal += this.incrementVal;
    }

    private void decrement() {
        this.startVal -= this.incrementVal;
    }
    private int getVal(){
        return val;
    }
    }
}

或者你也可以这样做......这就是你想要做的更多的东西,但是并不像#34; java"

import java.util.Scanner;

public class Main2 {

    static Scanner in = new Scanner(System.in);
    private static Count count;
    public static void main(String[] args) {
        setupCounter();
        for (;;) {
            counterControl();
        }

    }

    public static void setupCounter() {
        int startVal;
        int incrementVal;

        System.out.println("Please enter the starting value of the counter:");
        startVal = in.nextInt();
        System.out.println("Please enter the increment/decrement amount on the counter:");
        incrementVal = in.nextInt();

        count = new Count(startVal, incrementVal);
    }

    public static void counterControl() {
        String control;

        System.out.println("The following controls can be applied to the counter:");
        System.out.println("Increment - i");
        System.out.println("Decrement - d");
        System.out.println("Exit - x");

        control = in.nextLine();

        System.out.println(count.getVal());
        switch (control) {
            case "i":
                count.increment();
                break;
            case "d":
                count.decrement();
                break;
            case "x":
                System.exit(0);
                break;
        }
    }

    private static class Count {

        private int val, incrementVal;
        private Count(int startVal, int incrementVal) {
            this.val = startVal;
            this.incrementVal = incrementVal;
        }

        private void increment() {
            this.val += this.incrementVal;
        }

        private void decrement() {
            this.val -= this.incrementVal;
        }
        private int getVal(){
            return val;
        }
    }
}

答案 3 :(得分:0)

之前的答案很接近。 Amir只是忘了将setupCounter()的方法签名从void返回到Count。

基本上,您只需要一个静态变量来保持对您创建的Count对象的引用。为什么它需要是静态的?因为使用它的方法(例如counterControl()是静态的)。

public class Main {
  static Count count;

  ....

  public static void main(String[] args) {
    count = setupCounter();
    for (;;) {
      counterControl();
    }

  }

  public static Count setupCounter() {
    ...    
    return new Count(startVal, incrementVal);
  }