【问题标题】:Creating a getCounter() method to count up when the method is called创建一个 getCounter() 方法以在调用该方法时进行计数
【发布时间】:2017-03-15 12:32:11
【问题描述】:

我正在尝试创建一个 getCounter() 方法,每次调用 getCounter() 方法时都会向上计数。例如,如果该方法被调用 10 次,则输出应为:1,2,3...10。

这是我拥有的当前代码: 主类:

public class JavaLab5 {
    public static final int DEBUG = 0;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Student s[] = new Student[10];


        s[0] = new MathStudent("Smith");
        s[1] = new MathStudent("Jack");
        s[2] = new MathStudent("Victor");
        s[3] = new MathStudent("Mike");
        s[4] = new ScienceStudent("Dave");
        s[5] = new ScienceStudent("Oscar");
        s[6] = new ScienceStudent("Peter");
        s[7] = new ComputerStudent("Philip");
        s[8] = new ComputerStudent("Shaun");
        s[9] = new ComputerStudent("Scott");

        for (int loop = 0; loop < 10; loop++) {
            System.out.print(loop);
            System.out.println(s[loop].getSubjects());
        }

    }

}

学生班:

public class Student {
    String name;
    int count 
    public static int instances = 0;

    //Getters
    public  String getName() {
        return this.name;
    }

    // Setters
    public void setName(String name) {
        if (JavaLab5.DEBUG > 3) System.out.println("In Student.setName. Name = "+ name);
        this.name = name;
    }

    /**
     * Default constructor. Populates name,age and gender 
     * with defaults
     */
    public Student() {
        instances++;
        this.name = "Not Set";
    }

    /** 
     * Constructor with parameters 
     * @param name String with the name
    */
    public Student(String name) {
        this.name = name;
    }

    /**
     * Destructor
     * @throws Throwable 
     */
    protected void finalize() throws Throwable {
        //do finalization here
        instances--;
        super.finalize(); //not necessary if extending Object.
    } 

    public void getCounter() {
        for (int x = 0; x < 10; x++) {
        count++;
        System.out.println(count);
    }
    }
    public String toString () {
        return this.name; 
    }

    public String getSubjects() {
      return this.getSubjects();
    }
}

我的问题是如何实现这样的方法来从我的 Main 类中调用它。

【问题讨论】:

  • 有什么问题?
  • @YounesM 想在每次调用 getCounter() 方法时创建一个向上计数的计数器。
  • @anon1234 这是对您想要做的事情的描述,而不是问题。请提出问题。
  • 我投票结束,因为我仍然不清楚你在问什么。
  • @domdom 不清楚我在问什么,但您设法为我提供了答案,然后现在将其删除,并且所有这些用户都设法给了我答案。

标签: java count counter


【解决方案1】:

两种可能:

您希望每次调用该函数时都进行计数,并为每个对象创建一个新计数器

   public class Student {
        String name;
        int count =0; // every object of class Student will have its own `count`
        //rest of the declrataions / functions
        public void getCounter() {
            System.out.println(++count);
        }
}

您希望每次调用 getCounter 时都有一个计数器计数,而不管您拥有的类 Student 的对象数量如何

public class Student {
String name;
static int count = 0; //static so that multiple object instances of the class share the same `int count`
//rest of the declrataions / functions
public void getCounter() {
    System.out.println(++count);
}
}

【讨论】:

    【解决方案2】:

    如果你的问题是如何实现这样的方法,试试这个:

    private int myCounter = 0;
    
    //Use this line instead if you want a global counter for all Studen-Objects
    //private static int myCounter = 0;
    
    public void getCounter() {
      System.out.println(myCounter);
      myCounter++;
    }
    

    【讨论】:

    • 如何在主类中调用这个方法?
    • @anon1234 无意冒犯,但如果您不知道如何调用方法,您可能需要在查阅 SO 之前查阅一些书籍或教程。
    • 你需要一个 Student-Object 的引用来调用它,例如:s[0].getCounter();
    • 请注意,每个对象都有自己的计数器,如果您想为所有学生对象设置一个全局计数器,您需要将 myCounter 声明为静态
    【解决方案3】:

    你声明你想计算一个方法被调用了多少次。此外,您还想打印从1 开始到计数的方法调用次数的所有数字的序列。解决并发问题,我的解决方案是:

    public class CountTracker {
    
        private static final AtomicInteger counter = new AtomicInteger();
    
        public static int count() {
            return counter.incrementAndGet();
        }
    
        public static void printCounter() {
            int count = counter.get();
            if (count == 0) return;
            StringBuilder builder = new StringBuilder();
            for (int i = 1; i <= count; i++) builder.append(i).append(",");
            builder.deleteCharAt(builder.length() - 1);
            System.out.println(builder);
        }
    }
    

    方法count() 表示您要跟踪的方法。我不知道为什么您的解决方案确实包含 Student 类。

    【讨论】:

      【解决方案4】:

      只需删除 getCounter 方法中的 for 循环

      改变:

      public void getCounter() {
          for (int x = 0; x < 10; x++) {
          count++;
          System.out.println(count);
      }
      }
      

      到:

      public void getCounter() {
          System.out.println(count);
          count++;
      }
      

      【讨论】:

        猜你喜欢
        • 2012-09-28
        • 2020-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多