继承与多态


继承与多态

package com.demo;

public class JavaExtendsInherit {
    public static void main(String[] args) {
        Incomes[] incomes = new Incomes[]{
                new Incomes(3000),
                new Salary(7500),
                new StateCouncilSpecialAllowance(15000)
        };
        System.out.println(totalTax(incomes));
    }
    public static double totalTax(Incomes... incomes){
        double totalTax = 0;
        for(Incomes i : incomes){
            totalTax += i.getTax();
            System.out.println(totalTax);
        }
        return totalTax;
    }
}

class Incomes{
    protected double income;
    public Incomes(double income){
        this.income = income;
    }
    public double getTax(){
        return this.income * 0.1;
    }
}


class Salary extends Incomes{
    public Salary(double income) {
        super(income);
    }

    @Override
    public double getTax(){
        if (super.income > 5000) {
            return (super.income - 5000) * 0.2;
        }else {
            return 0;
        }
    }
}


class StateCouncilSpecialAllowance extends Incomes{
    public StateCouncilSpecialAllowance(double income) {
        super(income);
    }

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

相关