求助,看看我那个text类出现了什么问题
public class text {public static void main(String[] args) {
Manager boss = new Manager("zhang", 100);
boss.setBonus(33);
Employee[] staff = new Employee[2];
staff[0] = boss;
staff[1] = new Employee("deng", 100);
staff[2] = new Employee("li", 30);
for (Employee e : staff) {
System.out.println("name" + e.getName() + "salary" + e.getSalary());
}
}
class Employee {
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
}
class Manager extends Employee{
private double bonus;
public void setBonus(double bonus) {
this.bonus = bonus;
}
public Manager(String name, double salary, int i) {
super(name, salary);
this.bonus = bonus;
}
public double getBonus() {
return bonus;
}
}
} --------------------编程问答--------------------
Employee[] staff = new Employee[2];
staff[0] = boss;
staff[1] = new Employee("deng", 100);
staff[2] = new Employee("li", 30);
这里,staff数组明显长度为2,你却放了3个值,下面的我就没看了 --------------------编程问答--------------------
额,好吧,低级失误,可是去掉一个貌似还是有错 --------------------编程问答-------------------- 你的这个程序,Manager boss = new Manager("zhang", 100);里面有两个参数,儿你的构造函数却有三个参数,这都不错才怪 --------------------编程问答-------------------- 给你来个正确的:
public class text {
public static void main(String[] args) {
Manager boss = new Manager("zhang", 100);
boss.setBonus(33);
Employee[] staff = new Employee[3];
staff[0] = boss;
staff[1] = new Employee("deng", 100);
staff[2] = new Employee("li", 30);
for (int i=0;i<staff.length;i++) {
System.out.println("name" + staff[i].getName() + "salary" + staff[i].getSalary());
}
}
}
class Employee {
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
}
class Manager extends Employee {
private double bonus;
public void setBonus(double bonus) {
this.bonus = bonus;
}
public Manager(String name, double salary) {
super(name, salary);
this.bonus = salary;
}
public double getBonus() {
return bonus;
}
} --------------------编程问答--------------------
问题为什么出现在了循环输出的时候 --------------------编程问答-------------------- 循环哪儿是我个人习惯,你的错误主要有2个,一个是对象数组越界,第二是构造函数参数个数不对!你仔细看看吧,祝好! --------------------编程问答--------------------
是的
补充:Java , Java SE