在构造函数

时间:2016-09-02 20:51:25

标签: java

我正在研究以下从网站获得的这些代码(来源:https://gist.github.com/allisons/842546)。任何人都可以解释变量__processCalled的用法吗?我不明白为什么它被放在构造函数中以及它如何影响进程方法(在子类和超类中)。在我看来,这个布尔变量是赋值但实际上没有使用(如在if语句中:if(__processCalled){})。 非常感谢你的帮助!

//提供代码

public class Account {
    public boolean __processCalled; 
    public Account(Client c) {
        __processCalled = false;
    }

    public boolean process(Transaction t) {
        __processCalled = true;
        return t.value() > -100 && t.value() < 1000000;
    }

    public class Client {}

    public class Transaction {
        private int value;  

        public Transaction(int v) {
             value = v;
        }

        public int value() {
             return value;
        }
    }
}

//我的代码

public class FilteredAccount extends Account{

    private boolean __processCalled;
    private int filteredCount;
    private int transactionCount; 

    public FilteredAccount(Client c) {
       super(c);
       __processCalled = false; 
       filteredCount = 0;
       transactionCount = 0;
    }

    public boolean process(Transaction t){
        transactionCount++; 
        __processCalled = true;

        if(t.value == 0){
             filteredCount++;
             return true;
        }else{
             return super.process(t);
        }  
  }

  public double percentFiltered(){
      if(transactionCount == 0){
           return 0.0;
      }

      return filteredCount/transactionCount *100;
  }
}

0 个答案:

没有答案