如何知道从主类调用方法的次数?

时间:2015-12-02 09:45:57

标签: java

我的问题是找出从主类调用weight()方法的次数。我应该用totalWeightsMeasured()方法计算它。

代码的输出应为0,2,6。 (编辑//我之前在这里0,2,4,但输出应该是0,2,6)

但我只是不知道你怎么能算出它而且我试图谷歌和一切,但我不知道该怎么做。 (并且你不应该再添加任何实例变量

CLASS:

public class Reformatory
{
    private int weight;



    public int weight(Person person)
    {
        int weight = person.getWeight();

        // 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 0;
    }

}

MAIN:

public class Main
{

    public static void main(String[] args)
    {
        Reformatory eastHelsinkiReformatory = new Reformatory();

        Person brian = new Person("Brian", 1, 110, 7);
        Person pekka = new Person("Pekka", 33, 176, 85);

        System.out.println("total weights measured "+eastHelsinkiReformatory.totalWeightsMeasured());

        eastHelsinkiReformatory.weight(brian);
        eastHelsinkiReformatory.weight(pekka);

        System.out.println("total weights measured "+eastHelsinkiReformatory.totalWeightsMeasured());

        eastHelsinkiReformatory.weight(brian);
        eastHelsinkiReformatory.weight(brian);
        eastHelsinkiReformatory.weight(brian);
        eastHelsinkiReformatory.weight(brian);

        System.out.println("total weights measured "+eastHelsinkiReformatory.totalWeightsMeasured());
    }
}

4 个答案:

答案 0 :(得分:2)

诀窍是使用现有的实例变量权重(尚未使用)作为计数器。

public static IEnumerable<Tbl_Students> GetAllStudents()
{
    StudentDBEntities dataContext = new StudentDBEntities();

    var query = (from student in dataContext.Tbl_Students
                 join subject in dataContext.Tbl_Subjects on student.Roll_Number equals subject.Roll_Number
                 select new 
                 {
                     Roll_Number = student.Roll_Number,
                     FirstName = student.FirstName,
                     LastName = student.LastName,
                     Class = student.Class,
                     Gender = student.Gender,
                     Science = subject.Science,
                     Social = subject.Social,
                     Mathematics = subject.Mathematics,
                     Total = subject.Total
                 }).ToList().Select(s => new Tbl_Students
                 {
                     Roll_Number = s.Roll_Number,
                     FirstName = s.FirstName,
                     LastName = s.LastName,
                     Class = s.Class,
                     Gender = s.Gender,
                     Tbl_Subjects = new Tbl_Subjects ()
                     {
                          Science = s.Science,
                          Social = s.Social,
                          Mathematics = s.Mathematics,
                          Total = s.Total
                           Roll_Number = s.Roll_Number
                     };
                 });



    return query;
}

答案 1 :(得分:1)

正如Satya所指出的,引入一个static计数器变量如下:

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

答案 2 :(得分:0)

我认为我有一个解决方案,不需要你添加任何实例或静态变量。

由于在此代码块中,您已经有一个名为weight local 变量。

public int weight(Person person) {
    int weight = person.getWeight();

    /return the weight of the person
    return weight;
}

因此,在该方法的范围内,您有两个变量名为weight但一个是实例变量,一个是 local < / b>变量。您可以通过为实例变量编写this.weight或者为局部变量编写weight来区分这两者。

您需要做的就是将当前实例变量weight重命名为weightsMeasured,以便更清楚您所引用的变量。

然后只需将weightsMeasured++添加到weight(Person person)方法中,并return weightsMeasured方法添加totalWeightsMeasured()

所以最终的代码看起来像这样:

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

因此,我们只是重命名了未使用的现有实例变量,而不是添加任何新变量。

答案 3 :(得分:0)

以上答案的略微修改和缩短版本是:

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

}