2 dee p Dee_p 于 2015.06.08 15:38 提问

运行下面的代码出现图中的错误是为什么?大神请指教

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());
    }
}

}

图片说明

4个回答

caozhy
caozhy   Ds   Rxr 2015.06.08 15:40

staff[0]=(Employee)boss;

bulusli3
bulusli3   Ds   Rxr 2015.06.08 15:44

代码没有编译过。无法运行。

bulusli3
bulusli3   Ds   Rxr 2015.06.08 15:46

图片说明这两个变量不一致。

Dee_p
Dee_p 你指出的那些错误改过之后还是不行,但是当我将第一行把类放入包中的语句删除后就可以了,不知道这条语句有什么不妥呢?
2 年多之前 回复
caozhy
caozhy 眼尖!
2 年多之前 回复
danielinbiti
danielinbiti   Ds   Rxr 2015.06.08 15:43

有错误

 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;
}
}

自己对照什么错误

danielinbiti
danielinbiti 回复Dee_p: 我给你写的不是正确的嘛,都给你改了贴上去的
2 年多之前 回复
Dee_p
Dee_p 里面是有不少错误,已经改正了,不过不知道第一条将类打包的语句哪里有问题呢?正确的语句应该怎样写?请指教谢谢
2 年多之前 回复
tongyi55555
tongyi55555 正解。为啥不给出明确的答案呢。我觉得楼主肯定是在文本编辑器里写的,还是不要打击的好。
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片