被注释掉的代码为什么不能改变 salary
import *;import java.util.*;
public class ObjectStreamTest2 {
public static void main(String[] args) {
Employee[] staff = new Employee[3];
staff[0] = new Employee("马云",12345);
staff[1] = new Employee("马化腾",1000000000);
staff[2] = new Employee("李彦宏",1000000000);
int i;//变量在用之前必须要用付处置,在此没有付初值,在for()循环时付了初值
float bySalary;
try {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("./staff.dat"));
os.writeObject(staff);
os.close();
System.out.println("before change:");
for(i=0;i<staff.length;i++) {
staff[i].print();//直接把对象输出是错误的,要通过调用print()
}
ObjectInputStream in = new ObjectInputStream(new FileInputStream("./staff.dat"));
Employee[] newStaff = (Employee[])in.readObject();
in.close();
/*System.out.println("please enter bySalary: ");
DataInputStream ds = new DataInputStream(System.in);
bySalary = ds.readFloat();
System.in.skip(2);*/
for(i=0;i<newStaff.length;i++) {
newStaff[i].raiseSalary(100);
}
System.out.println("after change:");
for(i=0;i<newStaff.length;i++) {
newStaff[i].print();
}
} catch(Exception e) {
e.toString();
System.exit(-1);
}
}
static class Employee implements Serializable {
String name;
int salary;
public Employee(String name,int salary) {
this.name = name;
this.salary = salary;
}
public float raiseSalary(float bySalary) {
salary *= (1 + bySalary/100);
return salary;
}
public void print() {
System.out.println(name + ' ' + salary);
}
}
}