Java 如何知道一个方法从主类调用了多少次?
我的问题是找出从主类调用Java 如何知道一个方法从主类调用了多少次?,java,Java,我的问题是找出从主类调用weight()方法的次数。我应该用totalWeightsMeasured()方法计算它 代码的输出应为0,2,6。(编辑//我之前在这里使用了0,2,4,但是输出应该是0,2,6) 但我只是不知道你怎么计算它,我试着用谷歌搜索,但我就是不知道怎么做。(并且不应再添加任何实例变量) 类别: 主要内容: 正如Satya所指出的,引入一个静态计数器变量,如下所示: public class Reformatory { private static int weigh
weight()
方法的次数。我应该用totalWeightsMeasured()
方法计算它
代码的输出应为0,2,6。(编辑//我之前在这里使用了0,2,4,但是输出应该是0,2,6)
但我只是不知道你怎么计算它,我试着用谷歌搜索,但我就是不知道怎么做。(并且不应再添加任何实例变量)
类别:
主要内容:
正如Satya所指出的,引入一个
静态
计数器变量,如下所示:
public class Reformatory {
private static int weightMessurements = 0;
public int weight(Person person) {
// increment counter
weightMessurements++;
// messure weight
return person.getWeight();
}
public void feed(Person person) {
// increase weight
person.setWeight(person.getWeight() + 1);
}
public int totalWeightsMeasured() {
int result = weightMessurements;
// reset counter so that the output matches 0,2,4 instead of 0,2,6
weightMessurements = 0;
return result;
}
}
诀窍是使用现有的实例变量权重(尚未使用)作为计数器
public class Reformatory
{
private int weight;
public int weight(Person person)
{
int weight = person.getWeight();
this.weight++;
// return the weight of the person
return weight;
}
public void feed(Person person)
{
//that increases the weight of its parameter by one.
person.setWeight(person.getWeight() + 1);
}
public int totalWeightsMeasured()
{
return weight;
}
}
我想我有一个解决方案,它不需要您添加任何实例或静态变量 因为在这段代码中已经有一个名为
weight
的局部变量
public int weight(Person person) {
int weight = person.getWeight();
/return the weight of the person
return weight;
}
因此,在该方法的范围内,有两个变量称为weight
,但一个是实例变量,另一个是局部变量。您可以通过为实例变量编写this.weight
或仅为局部变量编写weight
来区分两者
您需要做的就是将当前实例变量weight
重命名为weightsMeasured
,以便更清楚地知道您引用的是哪个变量
然后只需将weightsMeasured++
添加到weight(Person)
方法中,并从totalWeightsMeasured()方法中返回weightsMeasured
最后的代码如下所示:
public class Reformatory
{
private int weightsMeasured; //Renamed instance variable
public int weight(Person person) {
weightsMeasured++; //instance variable incremented
int weight = person.getWeight(); //This is the local variable
//return the weight of the person
return weight;
}
public void feed(Person person) {
//that increases the weight of its parameter by one.
person.setWeight(person.getWeight() + 1);
}
public int totalWeightsMeasured() {
//return the number of times the method was called
return weightsMeasured;
}
}
因此,我们没有添加任何新变量,而是重命名了未使用的现有实例变量。上述答案的一个稍加修改的简短版本是:
public class Reformatory {
private int weight;
public int weight(Person person) {
this.weight++; //count the number of times when the weight gets measured.
return person.getWeight();
}
public int totalWeightMeasured() {
return weight; //return the number of times when the weight gets measured.
}
}保留一个计数器,每次调用该计数器时递增1。调用“weight()”方法的次数是多少?保持一个静态
计数器变量,在weight()
@manetsus中增加它:在这种情况下,答案是:不可能。我怀疑这就是OP想要的。@ParkerHalo是的,可能是。但我在想:不使用任何变量是否可能?@manetsus我试图按照同样的思路思考,但我一无所获。我能想到的唯一可能性是使用一个静态计数器变量或一个静态列表,每个权重都被添加到其中,然后计数就是列表的大小。静态变量与类本身相关,而不是与类的任何特定实例相关。我认为这值得一提,因为还没有人指出为什么这是答案。嗨!我试过这个,但是输出结果显示这个练习不需要静态变量。那么,没有静态变量,还有其他方法吗?(是的,这是一个家庭作业,它应该使用某种代码,但我不知道。)@Chrale从逻辑上看,需要在某个地方跟踪测量的数量。由于它不能是重整类上下文中的局部变量,因此它可以是实例变量或静态成员变量。如果它是一个实例变量,那么测量的数量将与特定的教养院相关。如果它是一个静态变量,那么这个数字将与所有改革者相关,或者不与任何改革者相关(取决于使用情况)。我发现,private int-weight代码>未在代码中使用-这可能是计数器的保留字段吗?
public class Reformatory
{
private int weightsMeasured; //Renamed instance variable
public int weight(Person person) {
weightsMeasured++; //instance variable incremented
int weight = person.getWeight(); //This is the local variable
//return the weight of the person
return weight;
}
public void feed(Person person) {
//that increases the weight of its parameter by one.
person.setWeight(person.getWeight() + 1);
}
public int totalWeightsMeasured() {
//return the number of times the method was called
return weightsMeasured;
}
}
public class Reformatory {
private int weight;
public int weight(Person person) {
this.weight++; //count the number of times when the weight gets measured.
return person.getWeight();
}
public int totalWeightMeasured() {
return weight; //return the number of times when the weight gets measured.
}