当前位置:编程学习 > JAVA >>

求助,看看我那个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个值,下面的我就没看了 --------------------编程问答--------------------
引用 1 楼 G_Sshine 的回复:
Java code?1234Employee[] 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;
}
} --------------------编程问答--------------------
引用 4 楼 yls_520_wl 的回复:
给你来个正确的:
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] =……


问题为什么出现在了循环输出的时候 --------------------编程问答-------------------- 循环哪儿是我个人习惯,你的错误主要有2个,一个是对象数组越界,第二是构造函数参数个数不对!你仔细看看吧,祝好! --------------------编程问答--------------------
引用 6 楼 yls_520_wl 的回复:
循环哪儿是我个人习惯,你的错误主要有2个,一个是对象数组越界,第二是构造函数参数个数不对!你仔细看看吧,祝好!

是的
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,