如题,不知道大家通常用的时候是不是也把对象放在收集类中啊?……………………………………………………………………………………
例如:
public class Employee extends Role {

private short salary;

public short getSalary() {
return this.salary;

public void setSalary(short sa) throws MyException {
if (sa<800)
throw (new MyException("工资太低了吧!"));
else
this.salary=sa;
}
public Employee() {
super();
ID=0;
this.salary=800;
}
public Employee(long id,String nm,int ag,String sx) {
super(nm,ag,sx);
ID=id;
this.salary=800;
}
}
_____________________________________________________________________另一个类中:
.......
Vector Saves = new Vector();
.......
if (e.getActionCommand()=="写入") {
if (!((textWriteID.getText().length()==0)||(textWriteID.getText().length()==0)||(textWriteID.getText().length()==0)||(textWriteID.getText().length()==0))) {
Saves.addElement(new Employee(Long.parseLong(textWriteID.getText()),txtWrite1.getText(),Integer.parseInt(txtWrite2.getText()),txtWrite3.getText()));
.......……………………………………………………………………………………………………………往出读数据的时候可以用:
((Employee) Saves.elementAt(k)).getName() 取出,可写的时候该怎么处理啊,比如要调用setSalary()来改变已经存储在Vector中的某个已实例化的Employee的salay属性该怎么办啊?望各位大哥大姐赐教~~~感激ing

解决方案 »

  1.   

    也是同样的方法呀,((Employee) Saves.elementAt(k)).setSalary(),Vector中存储的只是Employee实例的一个引用,类似于指针,真正的实例只有一个
      

  2.   

    是的,集合类里储存的是引用,你把引用用((Employee) Saves.elementAt(k))得出来,相当与
    Employee temp = ((Employee) Saves.elementAt(k));
    temp.setSalary();
      

  3.   

    晕菜~,加薪的按钮忘了加ActionListioner了~~~我说怎么按都没反应~
    感谢各位大哥指点 ~_~!  加分啦!