如何创建一个将字符串转换为布尔值的访问方法

时间:2015-07-21 15:51:09

标签: java arrays string methods boolean

如何创建一个将字符串转换为布尔值的访问方法? 下面的当前访问方法

protected boolean fullTime;

/**
 * Get the value of fullTime
 *
 * @return the value of fullTime
 */
public boolean isFullTime() {
    return fullTime;
}

/**
 * Set the value of fullTime
 *
 * @param fullTime new value of fullTime
 */
public void setFullTime(boolean fullTime) {
    this.fullTime = fullTime;
}

可以这样做吗

/**
 * set the coaches names
 * @param coaches as an array of strings
 */
public void setCoaches(String coaches)
{
    this.coaches = getStringAsArray(coaches);
}

public String getCoachesAsString()
{
    return getArrayAsString(coaches);
}

2 个答案:

答案 0 :(得分:0)

这是你想要的吗?

protected boolean fullTime;

public String isFullTimeAsString() {
    return String.valueOf(fullTime);
}

public void setFullTime(String fullTime) {
    this.fullTime = Boolean.parseBoolean(fullTime);
}

答案 1 :(得分:0)

喜欢这个吗?

protected boolean fullTime;

public String getFullTimeAsString(){
    return Boolean.toString(fullTime);
}

public void setFullTimeAsString(String fulltimeStr){
    fullTime = "true".equalsIgnoreCase(fulltimeStr);
}

编辑:

private static final String YES = "yes";
private static final String NO = "no";
protected boolean fullTime;


public String isFullTimeAsString() {
    return fullTime? YES: NO;
}

public void setFullTime(String fullTime) {
    this.fullTime =YES.equalsIgnoreCase(fullTime);
}