public class Main {
public static void main(String[] args) {
// TODO: 用接口给一个有工资收入和稿费收入的小伙伴算税:
Income[] incomes = new Income[] {new SalaryIncome(7500), new RoyaltyIncome(12000) };
double total = 0;
for (Income income:incomes) {
System.out.println(income.getClass());
total += income.getTax();
}
// TODO: System.out.println(total);
}
}
alienation
“在使用的时候,实例化的对象永远只能是某个具体的子类,但总是通过接口去引用它”
接口虽然没有实例化方法,但是可以通过子类向上转型的方式声明子类实例
上面income迭代的结果是
class SalaryIncome
class RoyaltyIncome