java是否支持多分派?如果不是,下面的代码如何工作? [英] Does java support multiple dispatch? If not how is the below code working?

查看:63
本文介绍了java是否支持多分派?如果不是,下面的代码如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java 是否支持多分派?如果不是,下面的代码是如何工作的?

Account.java

公共接口账号{public void calculateInterest();}

SavingsAccount.java

公共类 SavingsAccount 实现 Account{}

LoanAccount.java

公共类 LoanAccount 实现 Account{}

InterestCalculation.java

公共类兴趣计算{公共无效getInterestRate(帐户objAccount){System.out.println("账户利率计算");}public void getInterestRate(LoanAccountloanAccount){System.out.println("贷款账户利率为11.5%");}public void getInterestRate(SavingsAccount SavingAccount){System.out.println("储蓄账户利率为6.5%");}}

CalculateInterest.java

公共类CalculateInterest{public static void main(String[] args){InterestCalculation objIntCal = new InterestCalculation();objIntCal.getInterestRate(new LoanAccount());objIntCal.getInterestRate(new SavingsAccount());}}

输出

<前>贷款账户利率为 11.5%储蓄账户利率为 6.5%

解决方案

首先是一些术语

<块引用>

重载是创建多个方法的能力同名不同实现.

在 Java 中,这是基于参数的编译时类型执行的:带有 匹配签名,独立于参数的值.

<块引用>

覆盖 允许子类或子类提供已经由其中一个提供的方法的特定实现它的超类或父类.

在 Java 中,这是通过根据所引用对象的运行时(动态)类型确定要调用的方法来执行的.这是 Java 实现single dispatch 的方式,它是不要与重载混淆.

<块引用>

多分派意味着一个函数或方法可以基于运行时(动态)类型动态调度,或者,在更一般的情况是一些其他属性,它的多个属性争论.

Java 是否支持多分派?

在 Java 中,不直接支持多分派.

但是,您可以模拟多分派,方法是使用多层单分派结合重载.

这段代码是如何工作的?

请注意,此代码首先需要您为 LoanAccountSavingAccount 提供 calculateInterest() 的实现,即使它会不得在 POC 的其余部分中使用.

在您的类 InterestCalculation 中,您有方法 getInterestRate() 的三个不同的重载.它们每个都有一个不同的签名(例如参数类型).因此 main() 将根据参数的声明类型(从构造函数推导出来)调用正确的方法.

您可以优化单分派的使用并使 InterestCalculation 更加通用(例如,您可以添加更多 Account 的实现而无需更改它):

公共接口账号{public void calculateInterest();公共双 getInterest();公共字符串 getType();}...公共课利息计算{公共无效getInterestRate(帐户objAccount){System.out.print(+objAccount.getType()+"的利率是");System.out.print (objAccount.getInterest());System.out.println("%");}}

在线演示

Does java support multiple dispatch? If not how is the below code working?

Account.java

public interface Account 
{
    public void calculateInterest();
}

SavingsAccount.java

public class SavingsAccount implements Account
{

}

LoanAccount.java

public class LoanAccount implements Account
{

}

InterestCalculation.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%");
    }
}

CalculateInterest.java

public class CalculateInterest 
{
     public static void main(String[] args) 
     {
         InterestCalculation objIntCal = new InterestCalculation();
         objIntCal.getInterestRate(new LoanAccount());
         objIntCal.getInterestRate(new SavingsAccount());        
     }
}

Output

Interest rate for Loan Account is 11.5%
Interest rate for Savings Account is 6.5%

解决方案

First some terminology

Overloading is the ability to create multiple methods of the same name with different implementations.

In Java, this is performed based on the compile-time type of the arguments, : the method with the matching signature is used, independently of the argument's value.

Overriding allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.

In Java, this is performed by determining the method to be called depending on the run-time (dynamic) type of the object referred to. This is Java's way to implement single dispatch and it is not be confused with overloading.

Multiple dispatch means that a function or method can be dynamically dispatched based on the run-time (dynamic) type or, in the more general case some other attribute, of more than one of its arguments.

Does Java support multiple dispatch ?

In Java, there's no direct support for multiple dispatch.

However you can emulate multiple dispatch by using several layers of single dispatch combined with overloading.

How is this code working ?

Note that this code to work first requires that you provide an implementation of calculateInterest() for LoanAccount and SavingAccount, even if it will not be used in the rest of your POC.

In your class InterestCalculation , you have three distinct overloads of the method getInterestRate(). They each have a distinct signature (e.g. argument type). So main() will invoke the right method depending on the declared type of the argument (which is deduced from the constructor).

You could optimize your use of single dispatch and make InterestCalculation much more general (e.g. you could then add many more implementations of Account without having to change it) :

public interface Account 
{
    public void calculateInterest();
    public double getInterest(); 
    public String getType(); 
}
...
public class InterestCalculation 
{
    public void getInterestRate(Account objAccount)
    {
        System.out.print ("Interest Rate for "+objAccount.getType()+" is ");
        System.out.print (objAccount.getInterest());
        System.out.println (" %");
    }
}

Online demo

这篇关于java是否支持多分派?如果不是,下面的代码如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆