Discuss / Java / 我自己为了看懂算税的几十行代码的注释

我自己为了看懂算税的几十行代码的注释

Topic source

yy

#1 Created at ... [Delete] [Delete and Lock User]
    public static void main(String[] args) {
        // 给一个有普通收入、工资收入和享受国务院特殊津贴的小伙伴算税:
        Income[] incomes = new Income[] {//Income大类中创建incomes数组,以便于使用方法
            new Income(3000),	//Income方法,输入3000
            new Salary(7500),	//Salary方法,输入7500
            new StateCouncilSpecialAllowance(15000)//同理
        };
        System.out.println(totalTax(incomes));//totalTax(incomes)指的是
        									  //使用totalTax时输入incomes的数据
    }

    public static double totalTax(Income... incomes) {//totalTax计算时输入的应是incomes数组的数据
        double total = 0;
        for (Income income: incomes) {//for each循环,incomes数组中每一个元素都对应到Income大类里的income里
        							  //一一对应时,incomes数组中的数据含有方法,所以数据会输入到具体方法的income中
            total = total + income.getTax();//对输入的income数据使用getTax方法
        }
        return total;//返回总税
    }
}

class Income {
    protected double income;

    public Income(double income) {//创建Income方法,输入double型数据,返回income值
        this.income = income;
    }

    public double getTax() {//创建getTax方法,返回income值 * 0.1 作为Income类的税
        return income * 0.1; // 税率10%
    }
}

class Salary extends Income {//创建Salary类,并继承Income类
    public Salary(double income) {//创建Salary方法,输入double型数据,返回父类中定义的income值
        super(income); //此时继承父类的 实例 ,返回income值给Salary方法
    }

    @Override   //覆写,在Salary类下的税
    public double getTax() {
        if (income <= 5000) {
            return 0;
        }
        return (income - 5000) * 0.2;
    }
}

class StateCouncilSpecialAllowance extends Income {//创建类
    public StateCouncilSpecialAllowance(double income) {//创建方法
        super(income);//返回值
    }

    @Override
    public double getTax() {
        return 0;
    }
}

  • 1

Reply