java支持多分派吗?如果不是,下面的代码是如何工作的?
账户.java
public interface Account
{
public void calculateInterest();
}
SavingsAccount.java
public class SavingsAccount implements Account
{
}
LoanAccount.java
public class LoanAccount implements Account
{
}
利息计算.java
public class InterestCalculation
{
public void getInterestRate(Account objAccount)
{
System.out.println("Interest Rate Calculation for Accounts");
}
public void getInterestRate(LoanAccount loanAccount)
{
System.out.println("Interest rate for Loan Account is 11.5%");
}
public void getInterestRate(SavingsAccount savingAccount)
{
System.out.println("Interest rate for Savings Account is 6.5%");
}
}
计算利息.java
public class CalculateInterest
{
public static void main(String[] args)
{
InterestCalculation objIntCal = new InterestCalculation();
objIntCal.getInterestRate(new LoanAccount());
objIntCal.getInterestRate(new SavingsAccount());
}
}
输出
贷款账户利率为 11.5% 储蓄账户利率为 6.5%