通过使获取器和设置器访问私有字段来封装它们。
public class Player { /*时 代 Java - nowjava.com 提供*/ private String firstName = null; private String lastName = null; private String position = null; private int status = -1; public Player(){ } public Player(String position, int status){ this.position = position; this.status = status; } protected String playerStatus(){ String returnValue = null; /** from * N o w J a v a . c o m - 时 代 Java **/ switch(getStatus()){ case 0: returnValue = "ACTIVE"; case 1: returnValue = "INACTIVE"; case 2: returnValue = "INJURY"; default: returnValue = "ON_BENCH"; } return returnValue; } public String playerString(){ return getFirstName() + " " + getLastName() + " - " + getPosition(); } /** * @return the firstName */ public String getFirstName() { return firstName; } /** * @param firstName the firstName to set */ public void setFirstName(String firstName) { this.firstName = firstName; } /** * @return the lastName */ public String getLastName() { return lastName; } /** * @param lastName the lastName to set */ public void setLastName(String lastName) { this.lastName = lastName; } /** * @return the position */ public String getPosition() { return position; } /** * @param position the position to set */ public void setPosition(String