***我遇到了正确创建add()subtract()和divide()方法的问题,也许我正在过度思考它。我如何正确返回所需的对象。我的IDE说“无法解析符号温度”。我正确地称这个'这个'了吗?
***我知道add和divide方法是不完整的,如果我可以在完成subtract()方法时获得帮助,那么一切都应该落实到位。
import javax.naming.StringRefAddr;
/**
* Created by Makoto on 2/4/2016.
*/
public class Temperature {
public double temp;
public char type;
Temperature() {
temp = 0.0;
type = 'o';
}
Temperature( double temperature, char tempType){
temperature = temp;
tempType = type;
}
public Temperature toFahrenheit() {
type.equalsIgnoreCase();
switch (type) {
case 'C':
break;
case 'K':
break;
default:
System.out.println("Can not convert.");
break;
}
return this.Temperature;
}
public Temperature toCelsius() {
type.equalsIgnoreCase();
switch (type) {
case 'F':
break;
case 'K':
break;
default:
System.out.println("Can not convert.");
break;
}
return this.Temperature;
}
public Temperature toKelvin() {
type.equalsIgnoreCase();
switch (type) {
case 'C':
break;
case 'F':
break;
default:
System.out.println("Can not convert.");
break;
}
return this.Temperature;
}
public Temperature add(Temperature x){
this.Temperature + x;
return this.Temperature;
}
public Temperature subtract(Temperature x){
this.Temperature = this.temp - x.temp;
return this.Temperature;
}
public Temperature divide(int x){
this.Temperature = /x;
return this.Temperature;
}
}
答案 0 :(得分:0)
您只需要退货。
import javax.naming.StringRefAddr;
/**
* Created by Makoto on 2/4/2016.
*/
public class Temperature {
public double temp;
public char type;
Temperature() {
temp = 0.0;
type = 'o';
}
Temperature( double temperature, char tempType){
temperature = temp;
tempType = type;
}
public Temperature toFahrenheit() {
type.equalsIgnoreCase();
switch (type) {
case 'C':
break;
case 'K':
break;
default:
System.out.println("Can not convert.");
break;
}
return this;
}
public Temperature toCelsius() {
type.equalsIgnoreCase();
switch (type) {
case 'F':
break;
case 'K':
break;
default:
System.out.println("Can not convert.");
break;
}
return this;
}
public Temperature toKelvin() {
type.equalsIgnoreCase();
switch (type) {
case 'C':
break;
case 'F':
break;
default:
System.out.println("Can not convert.");
break;
}
return this;
}
public Temperature add(Temperature x){
this.temp + x.temp;
return this;
}
public Temperature subtract(Temperature x){
this.temp = this.temp - x.temp;
return this;
}
public Temperature divide(int x){
this.temp = /x;
return this;
}
}
答案 1 :(得分:0)
this.Temperature = this.temp - x.temp;
不是Java中的有效语句。您试图将Temperature
(它本身不是对象)设置为值。您在此处尝试执行的操作是对temp
变量执行操作,然后返回this
。通过返回this
,调用对象现在将具有带有更新字段的Temperature对象。
例如:
public Temperature subtract(Temperature x){
temp = temp - x.temp;
return this;
}
上面的代码将更新此温度对象的临时,然后返回自身。然后,调用对象可以通过调用returnedTemperatureObject.temp
来访问它。