我正在尝试创建一个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类中调用它。
答案 0 :(得分:0)
如果你的问题是如何实现这样的方法,试试这个:
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++;
}
答案 1 :(得分:0)
两种可能性:
您希望在每次调用该函数时计算并为每个对象设置一个新的计数器
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 :(得分:0)
只需在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++;
}
答案 3 :(得分:0)
您声明您想要计算方法被调用的次数。此外,您希望打印从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
。