[英]char cannot be dereferenced
我正在做一個需要使用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;
}
}
您不能在原始類型上調用方法,而char是原始類型!
采用
if (scale == 'K')
等等比較字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.