小弟新学Java,碰到了一个这样的问题,JDK是:JDK 8,速度亮代码如下:
import java.util.ArrayList;
import java.util.Iterator;
class Employee
{
protected int month;
protected String name;
public double getSalary(int month) {
if(month == this.month) {
return 100;
} else {
return 0;
}
}
public Employee(String name, int month) {
this.month = month;
this.name = name;
}
public void print(int m) {
System.out.println(name + "'s salary: " + getSalary(m));
}
}
class SalariedEmployee extends Employee
{
protected double monthSalary;
public static final int overtimeSalary = 2000;
public SalariedEmployee(String name, int month, double monthSalary) {
super(name, month);
this.monthSalary = monthSalary;
}
public double getSalary(int month) {
return monthSalary + super.getSalary(month) + overtimeSalary;
}
public void print(int m) {
System.out.println(name + "'s monthSalary: " + getSalary(m));
}
}
class HourlyEmployee extends Employee
{
protected double hourlySalary;
protected int hours;
public HourlyEmployee(String name, int month, double salary, int hours) {
super(name, month);
hourlySalary = salary;
this.hours = hours;
}
public double getSalary(int month) {
if (hours <= 160)
{
return hourlySalary * hours;
} else {
return hourlySalary * 160 + hourlySalary * (hours - 160) * 1.5;
}
}
public void print(int m) {
System.out.println(name + "'s Salary: " + getSalary(m));
}
}
class SalesEmployee extends Employee
{
protected double salesAmount;
protected double percentage;
public SalesEmployee(String n, int m, double sa, double per) {
super(n, m);
salesAmount = sa;
percentage = per;
}
public double getSalary(int m) {
return salesAmount * percentage + super.getSalary(m);
}
public void print(int m) {
System.out.println(name + "'s Salary: " + getSalary(m));
}
}
class BasePlusSalesEmployee extends SalesEmployee
{
protected double baseSalary;
public static final int overtimeSalary = 1000;
public BasePlusSalesEmployee(String n, int m, double sa, double per, double baseSalary) {
super(n, m, sa, per);
this.baseSalary = baseSalary;
}
public double getSalary(int m) {
return baseSalary + super.getSalary(m) + overtimeSalary;
}
public void print(int m) {
System.out.println(name + "'s Salary: " + getSalary(m));
}
}
class Day06
{
public static void main(String[] args) {
ArrayList<Employee> al = new ArrayList<Employee>();
al.add(new Employee("Liyi", 12));
al.add(new SalariedEmployee("LinMing", 11, 3000));
al.add(new HourlyEmployee("LinDong", 9, 20, 180));
al.add(new SalesEmployee("ShiHao", 8, 100000, 0.05));
al.add(new BasePlusSalesEmployee("ShenJi", 8, 100000, 0.05, 1000));
al.get(0).print(12);
al.get(1).print(10);
al.get(2).print(8);
al.get(3).print(8);
al.get(4).print(8);
int overtimeCost = 0;
for (Iterator it = al.iterator(); it.hasNext(); )
{
Employee ee = it.next();
if (ee instanceof SalariedEmployee)
{
overtimeCost += SalariedEmployee.overtimeSalary;
} else if (ee instanceof BasePlusSalesEmployee)
{
overtimeCost += BasePlusSalesEmployee.overtimeSalary;
}
}
System.out.println("the cost of company is: " + overtimeCost);
}
}
求解答!
------解决思路----------------------
需要强制类型转换
(Employee)it.next()
------解决思路----------------------
1.首先Iterator里面的元素,初始肯定是Object类型的(不然你怎么往里面放对象进去?),正因为所有类都继承Object,它才可以安全的被放在"Iterator里面",没有什么特殊操作的话,里面的类型不会变吧~第二,如果因为你填充类Employee的几个实例(包括子类实例)进去,按照你的意思,假如它自动帮你在内部已经转型好了,那么假如还有个Employee的父类的实例要放进去,那么这里除非投放进去时弄向下转型,否则不能放进去吧~
第三,初学的话,不要钻的那么深,这种精神是好,但是等你把面扩大点,工作后再来玩这些更合适.目前的话,多熟悉api吧,能够熟练运用即可,如果要玩深点的话,这问题涉及到java的设计了吧(我的认为),默认返回Object类型,一定意义上是为了确保安全.