public void incEmployee(Employee anEmployee)
{
// Increment the age of the employee
ageField.setText(String.valueOf((anEmployee.incAge())));
}
public void decEmployee(Employee anEmployee)
{
// Decrement the age of the employee
ageField.setText(String.valueOf((anEmployee.decAge())));
}这样就能显示出你年龄的修改来了。
{
// Increment the age of the employee
ageField.setText(String.valueOf((anEmployee.incAge())));
}
public void decEmployee(Employee anEmployee)
{
// Decrement the age of the employee
ageField.setText(String.valueOf((anEmployee.decAge())));
}这样就能显示出你年龄的修改来了。
//将anEmployee指向了新建的对象anotherEmployee了,当然一直equals了,
//怎么可能会对呢?还有,既然要修改数据,应该是对现有的对象的属性进行
//修改,为何再去新建一个Emplyee的对象呢???
虚线以下部分:/*===========================================================================
以下是实现方法
===========================================================================*/
// When the "Add" button is pressed, this method is invoked. It is passed
// the name and the age as entered in GUI. This method must create a new
// instance of employee using the supplied values. The newly created employee
// object is then to be returned.
public Employee setupEmployee(String aName,int anAge)
{
// Create a new Instance of Employee. Initialize it with the name and
// age supplied.
Employee anEmployee=new Employee(aName,anAge);
// Return the newly created employee object;
return anEmployee;
}// This method is invoked every time an employee object must be displayed in
// the list. This method should create a display string using the data
// obtained from the employee object parameter. public String setupDisplayString(Employee anEmployee)
{
// Create a string and assign the display text to it. The display text
// should be created using data supplied by the employee object passed into
// this method.
String displayName=anEmployee.getName();
return displayName;
}// This method is invoked every time the modify employee button is pressed.
// This method must replace the values of the employee parameter with the
// values passed to the method. public void updateEmployee(Employee anEmployee, String aNewName, int aNewAge)
{
//Employee anotherEmployee=new Employee(aNewName,aNewAge);
//既然是要修改,那你怎么又新建了一个对象呢?应该修改传进来的anEmployee的相关属性才对
//anEmployee=anotherEmployee;
//将anEmployee指向了新建的对象anotherEmployee了,当然一直Equals
//if(anEmployee.equals(anotherEmployee))
// System.out.println("they are equal!");
if(anEmployee.getName().equals(aNewName))
{
System.out.println("they are equal!");
return;
}else
anEmployee.setValue(aNewName,aNewAge);
}// public void incEmployee(Employee anEmployee)
{
// Increment the age of the employee
ageField.setText(String.valueOf((anEmployee.incAge())));
}
public void decEmployee(Employee anEmployee)
{
// Decrement the age of the employee
ageField.setText(String.valueOf((anEmployee.decAge())));
}//*****************************************
//**Employee类也要修改,加入修改属性的方法**
//*****************************************
class Employee
{
// Declare two instance variables. One to hold the employee's name
// and the other to hold the employee's age
private String name;
private int age; // Provide a constructor which takes the name and age as parameters
public Employee(String n,int a)
{
setValue(n,a);
}
// Provide get and set methods for both name and age.
// Note: The method names should take the form setName, getName, setAge, and getAge
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
// Provide an incAge method which increments the age of the employee when called
public int incAge()
{
return age+=1;
}
// Provide a decAge method which decrements the age of the employee when called.
public int decAge()
{
return age-=1;
}
public void setValue(String n,int a)
{
name=n;
age=a;
}
}
不过要是Employee类还包含更多的属性,修改就有意义多了。同意楼上的观点。