我想知道是否有办法监控我的方法的活动。
以下是要求
CheckingAccount将收取交易费用。 每个月有5个免费交易。每笔额外交易将从支票账户中扣除3美元。(提示:如果一个月内超过5个,您需要添加一种从支票账户中扣除费用的方法。您需要修改存款和取款方式,如下所示。好吧,你需要添加一个实例字段来记住交易次数。你可能需要定义两个常数,一个用于允许的免费交易数量,另一个用于每次额外交易的费用。)
所以在我的主要对象中,我需要监视对象被调用/使用的次数?
这是我的主要课程
公共类BankAccount {
public double balance = 0.0; //init bal
public BankAccount(){ //create new constructor
}
public BankAccount(double bal) throws Exception { //create new constructor
if (bal << 0)
throw new IllegalArgumentException();
else
balance = bal;
}
public double getBalance(){ //return balance
return balance;
}
public void deposit(double amount) throws Exception { //put money into account
if (amount < 0)
throw new IllegalArgumentException();
balance = balance + amount;
}
public void withdraw(double amount) throws Exception { //take money from account
if(amount < 0)
throw new IllegalArgumentException();
if(amount > balance)
throw new InsufficientFundsException();
balance = balance-amount;
}
public void transfer(double amount, BankAccount other) throws Exception //transfer money to other acc
{
this.withdraw(amount);
other.deposit(amount);
}
public String toString(){ //give info
String s = ("The balance is: $" + this.getBalance() +".");
return s;
}
}
我还定义了自己的InsufficientFundsException
第二个要求是
SavingAccount(类)将按固定利率赚取收入。 (提示:您需要添加一个方法来每月增加兴趣。此外,您还需要为利率添加一个实例字段。)
如何在JVM上完成此操作?我之前从未使用过基于时间的命令。
提前致谢。
答案 0 :(得分:0)
不,没有内置的方法来监视方法的调用次数。您需要遵循作业中给出的指导原则:
您还需要修改存款和取款方式 需要添加一个实例字段来记住事务数量。
假设CheckingAccount是BankAccount的子类,请覆盖方法并在方法中添加代码来计算调用。
另一个答案是正确的,当然,只与你的任务无关:有许多分析工具可以计算每种方法被调用的次数。 HPROF只是其中的一个。据我所知,它们与你的作业无关。
对于SavingAccount类,我认为不需要基于时间的命令。假设有人会在每个月末调用您的方法来增加兴趣。出于测试目的,您可以根据需要随时调用它(并且只会显示您快速致富: - )
答案 1 :(得分:-2)
您可以使用Oracle HPROF工具