递归方法 [英] Recursive method

查看:59
本文介绍了递归方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以包含组件或组合的数组列表,每个组件或组合都有一个我想要获取值的税字段.此方法的目的是获取所有组件和组合的应缴税款总额在数组列表中.一个组件也可以包含一个组合.问题是当一个组件包含一个组合时,我的方法没有获取组合中的值

I have an array list that could contain component or composite and each component or composite has a tax field that i would like to get the value.The purpose of this method is to get the total tax due by all the components and composites in the array list.A component can also contain a composite .The problem is when a component contains a composite ,My method does not get the value in the composite

ArrayList allprinceSubjects = new ArrayList();
public double calculateTaxDueByComponents(){
    double totaltaxdue=0;
    Iterator iterator = vassalsanddukes.iterator();
    while(iterator.hasNext()){
        RiruritaniaSubjects vassalandduke=(RiruritaniaSubjects) iterator.next();
        totaltaxdue+=vassalandduke.getTaxDue();
        vassalandduke.calculateTaxDueByComponents();


    }
    return totaltaxdue;
}

推荐答案

不确定您的业务逻辑是如何实现的,但我认为以下行需要更改:

Not sure how your business logic are implemented, but I think the following line needs changing:

vassalandduke.calculateTaxDueByComponents();

改为:

totaltaxdue += vassalandduke.calculateTaxDueByComponents();

这篇关于递归方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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