多态是指,针对某个类型的方法调用,其真正执行的方法取决于运行时期实际类型的方法。
public class Main {public static void main(String[] args) {Person p = new Student();p.run(); // 应该打印Person.run还是Student.run?}}class Person {public void run() {System.out.println("Person.run");}}class Student extends Person {@Overridepublic void run() {System.out.println("Student.run");}}
public class Polymorphic {public static void main(String[] args) {// 给一个有普通收入、工资收入和享受国务院特殊津贴的小伙伴算税:Income[] incomes = new Income[] {new Income(3000),new Salary(7500),new StateCouncilSpecialAllowance(15000)};System.out.println(totalTax(incomes));}public static double totalTax(Income... incomes) {double total = 0;for (Income income: incomes) {total = total + income.getTax();}return total;}}class Income {protected final double income;public Income(double income) {this.income = income;}public double getTax() {return income * 0.1; // 税率10%}}class Salary extends Income {//重载方法,方法签名不一致,这是一个新方法。public Salary(double income) {super(income);}@Overridepublic double getTax() {if (income <= 5000) {return 0;}return (income - 5000) * 0.2;}}class StateCouncilSpecialAllowance extends Income {public StateCouncilSpecialAllowance(double income) {super(income);}@Overridepublic double getTax() {return 0;}}
观察totalTax()方法:利用多态,totalTax()方法只需要和Income打交道,它完全不需要知道Salary和StateCouncilSpecialAllowance的存在,就可以正确计算出总的税。如果我们要新增一种稿费收入,只需要从Income派生,然后正确覆写getTax()方法就可以。把新的类型传入totalTax(),不需要修改任何代码。
可见,多态具有一个非常强大的功能,就是允许添加更多类型的子类实现功能扩展,却不需要修改基于父类的代码。
