问题描述
- 运行下面的代码出现图中的错误是为什么?大神请指教
-
package inheritance;
import java.util.Date;
import java.util.GregorianCalendar;
public class Employee {
private String name;
private double salary;
private Date hireday;public Employee(String n,double s,int year,int month,int day) { name=n; salary=s; GregorianCalendar calendar=new GregorianCalendar(year,month-1,day); hireday=calender.getTime(); } public String getName() { return name; } public double getSalary() { return salary; } public Date getHireday() { return hireday; } public void raiseSalary(double byPercent) { double raise=salary*byPercent/100; salary+=raise; }
}
package inheritance;
public class Manager extends Employee {
private double bonus;
public Manager(String n,double s,int year,int month,int day)
{
super(n,s,year,month,day);
bonus=0;
}
public double getSalary();
{
double baseSalary=super.getSalary();
return baseSalary+bonus;
}
public void setBonus(double b)
{
bonus=b;
}
}package inheritance;
public class ManagerTest {
public static void main (String[] args){
Manager boss=new Manager("Born",10000,1991,7,17);
boss.setBonus(8000);Employee[] staff=new Employee[3]; staff[0]=boss; staff[1]=new Employee("jack",6000,1990,3,6); staff[2]=new Employee("Marry",7000,1992,4,8); for(int i=0;i<staff.length;i++) { System.out.println("name="+staff[0].getName()+"salary="+staff[i].getSalary()); } }
}
解决方案
staff[0]=(Employee)boss;
解决方案二:
代码没有编译过。无法运行。
解决方案四:
有错误
public Employee(String n,double s,int year,int month,int day)
{
name=n;
salary=s;
GregorianCalendar calendar=new GregorianCalendar(year,month-1,day);
hireday=calendar.getTime();
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public Date getHireday()
{
return hireday;
}
public void raiseSalary(double byPercent)
{
double raise=salary*byPercent/100;
salary+=raise;
}
}
public class Manager extends Employee {
private double bonus;
public Manager(String n,double s,int year,int month,int day)
{
super(n,s,year,month,day);
bonus=0;
}
public double getSalary()
{
double baseSalary=super.getSalary();
return baseSalary+bonus;
}
public void setBonus(double b)
{
bonus=b;
}
}
自己对照什么错误
时间: 2024-11-02 07:36:25