1. // 多态
    2. /*
    3. * 如果方法签名不同,就是Overload
    4. * 方法签名相同,并且返回值也相同,就是Override
    5. * 标志重写父类方法*/
    6. public class Object1 {
    7. public static void main(String[] args) {
    8. // 给一个有普通收入、工资收入和享受国务院特殊津贴的小伙伴算税:
    9. Income[] incomes = new Income[] {
    10. new Income(3000),
    11. new Salary(7500),
    12. new StateCouncilSpecialAllowance(15000)
    13. };
    14. System.out.println(totalTax(incomes));
    15. }
    16. public static double totalTax(Income... incomes) {
    17. double total = 0;
    18. for (Income income: incomes) {
    19. total = total + income.getTax();
    20. }
    21. return total;
    22. }
    23. }
    24. class Income {
    25. //final class Income { // final作用是:该类不能够被继承
    26. protected double income;
    27. //public final String name="小帅"; // final作用是:该字段初始化之后不能够被赋值
    28. public Income(double income) {
    29. this.income = income;
    30. }
    31. public double getTax() {
    32. //public final double getTax() { // final作用是该父类方法不能被重写
    33. return income * 0.1; // 税率10%
    34. }
    35. }
    36. class Salary extends Income {
    37. public Salary(double income) {
    38. super(income);
    39. }
    40. @Override
    41. public double getTax() {
    42. if (income <= 5000) {
    43. return 0;
    44. }
    45. return (income - 5000) * 0.2;
    46. }
    47. }
    48. class StateCouncilSpecialAllowance extends Income {
    49. public StateCouncilSpecialAllowance(double income) {
    50. super(income);
    51. }
    52. @Override
    53. public double getTax() {
    54. return 0;
    55. }
    56. }