编写Java程序时出现问题,求高手解答

DunnLin 2015-04-07 04:45:50
新手入门java,编写继承程序时出现了错误


import java.util.Date;
import java.util.GregorianCalendar;

import com.lesson.Employee.Manager;

public class EmployeeTest
{

public static void main(String[] args)
{
// TODO Auto-generated method stub
Manager boss = new Manager("Wade", 8000, 1987, 12, 15);
boss.setBonus(500);

Employee[] staff = new Employee[3];
staff[0] = boss;
staff[1] = new Employee("James", 5000,1987, 11, 23);
staff[2] = new Employee("Kobe", 6000,1988, 10, 23);

for(Employee e : staff)
e.raiseSalary(5);

for(Employee e : staff)
System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay());

}

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

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

}
}

它显示了这个错误:No enclosing instance of type Employee is accessible. Must qualify the allocation with an enclosing instance of type Employee (e.g. x.new A() where x is an instance of Employee).
...全文
206 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
可乐罐 2015-04-07
  • 打赏
  • 举报
回复
翻译成中文就是“没有可访问的内部类Employee的实例” 这个情况是你在main函数中调用了内部类Employee。但是,main函数是静态方法(static)实例化内部类貌似不行。 我想到了两个办法 1、将Employee类申明为static; 2、将Employee类改成外部类;
fondOfJava 2015-04-07
  • 打赏
  • 举报
回复
自己解决呀,不是很难。

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧