我正在做一个需要使用get和set方法转换温度的任务。但是,当我尝试编写setMethod时,U出现错误,提示“无法取消引用char”。这是我的代码;
public void setTemp(double temp, char scale){
// - sets the objects temperature to that specified using the
// specified scale ('K', 'C' or 'F')
if (scale.isLetter("K")){
temp = temp + 273;
}else if (scale.isLetter("C")){
temp = temp;
}else if (scale.isLetter("F")){
temp = ((9 * temp) / 5 ) + 32;
}
}
基元(例如char
)没有方法。不过,看来您只是在寻找一个简单的相等性测试。
编辑:
正如Elliott Frisch在评论中指出的那样,您需要使用它this.temp
来引用您的数据成员,因为temp
参数将其隐藏了:
public void setTemp(double temp, char scale){
// - sets the objects temperature to that specified using the
// specified scale ('K', 'C' or 'F')
if (scale == 'K'){
this.temp = temp + 273;
} else if (scale == 'C') {
this.temp = temp;
} else if (scale == 'F') {
this.temp = ((9 * temp) / 5 ) + 32;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句