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      2015.06.08 15:40

staff[0]=(Employee)boss;

bulusli3      2015.06.08 15:44

bulusli3      2015.06.08 15:46

Dee_p 你指出的那些错误改过之后还是不行，但是当我将第一行把类放入包中的语句删除后就可以了，不知道这条语句有什么不妥呢？

caozhy 眼尖！

danielinbiti      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 回复Dee_p: 我给你写的不是正确的嘛，都给你改了贴上去的

Dee_p 里面是有不少错误，已经改正了，不过不知道第一条将类打包的语句哪里有问题呢？正确的语句应该怎样写？请指教谢谢

tongyi55555 正解。为啥不给出明确的答案呢。我觉得楼主肯定是在文本编辑器里写的，还是不要打击的好。