我编写了这段代码,但它不会编译,因为类温度已经定义了它。
已定义类型温度
如何解决?
package exercise11;
public class working {
public static void main(String[] args) {
temperature freezing = new temperature(32, 0, 0);
temperature boiling = new temperature(212, 100, 33);
temperature human = new temperature(98.6, 37, 12.21);
System.out.println("water freezes at " + freezing.getFahrenheit() + " Fahrenheit, " + freezing.getCelsius() +
" Celisus, and " + freezing.getNewton() + " Newton");
System.out.println("water boils at " + boiling.getFahrenheit() + " Fahrenheit, " + boiling.getCelsius() +
" Celisus, and " + boiling.getNewton() + " Newton");
System.out.println("human body temperature is " + human.getFahrenheit() + " Fahrenheit, " + human.getCelsius() +
" Celisus, and " + human.getNewton() + " Newton");
}
}
class temperature {
public temperature(double f, double b, double h) {
}
double Fahrenheit;
double Celsius;
double Newton;
public void setTemp(double Fahrenheit, double Celsius, double Newton) {
setFahrenheit(Fahrenheit);
setCelsius(Celsius);
setNewton(Newton);
}
public double getNewton() {
return Newton;
}
public double getCelsius() {
return Celsius;
}
public double getFahrenheit() {
return Fahrenheit;
}
public void setFahrenheit(double Fahrenheittemp) {
Fahrenheit = Fahrenheittemp;
}
public void setCelsius(double Celsiustemp) {
Celsius = Celsiustemp;
}
public void setNewton(double Newtontemp) {
Newton = Newtontemp;
}
}
答案 0 :(得分:1)
可能不止一次定义温度。
只需将班级名称从“温度”更改为“温度”或其他等级。不要忘记构造函数和你使用它的地方等。
答案 1 :(得分:0)
它为我工作。您可能希望在构造函数中指定Fahrenheit,Celsius和Newton:
public class working {
public static void main(String[] args) {
temperature freezing = new temperature(32,0,0);
temperature boiling = new temperature(212,100,33);
temperature human = new temperature(98.6,37,12.21);
System.out.println("water freezes at " + freezing.getFahrenheit() + " Fahrenheit, " + freezing.getCelsius() + " Celisus, and " + freezing.getNewton() + " Newton");
System.out.println("water boils at " + boiling.getFahrenheit() + " Fahrenheit, " + boiling.getCelsius() + " Celisus, and " + boiling.getNewton() + " Newton");
System.out.println("human body temperature is " + human.getFahrenheit() + " Fahrenheit, " + human.getCelsius() + " Celisus, and " + human.getNewton() + " Newton");
}
}
class temperature {
public temperature (double f, double b, double h) {
Fahrenheit = f; // <==== ADD?
Celsius = b; // <==== ADD?
Newton = h; // <==== ADD?
}
double Fahrenheit;
double Celsius;
double Newton;
public void setTemp(double Fahrenheit, double Celsius, double Newton){
setFahrenheit(Fahrenheit);
setCelsius(Celsius);
setNewton(Newton);
}
public double getNewton() {
return Newton;
}
public double getCelsius() {
return Celsius;
}
public double getFahrenheit() {
return Fahrenheit;
}
public void setFahrenheit(double Fahrenheittemp){
Fahrenheit = Fahrenheittemp;
}
public void setCelsius(double Celsiustemp){
Celsius = Celsiustemp;
}
public void setNewton(double Newtontemp){
Newton = Newtontemp;
}
}
答案 2 :(得分:0)
如果这一切都在一个文件中,则可能不是唯一正在编译的文件。
检查其他文件,看看你是否定义了两次温度等级。
---顺便说一句,评论你的设计---
如果您已经了解了多态性,请认识到温度是特定测量系统Fahrenheit,Celsius和Newton的推广。
因此可能会想要创建一个温度界面
public interface Temperature {
public double getNewton();
public double getCelsius();
public double getFarenheit();
}
有三个实施类
public class Netwon implements Temperature {
private double temp;
public Newton(double value) {
this.temp = value;
}
public double getNewton() {
return temp;
}
public double getCelsius() {
return (3*temp);
}
public double getFarenheit() {
return (getCelsius() * 9/5) + 32;
}
}
另外两种实现相互适应的温度。这具有期望的效果,即不会给将来(您和其他人)带来必须手动计算三个正确匹配值的负担。而你只是做
public Temperature temp = new Celsius(100);
System.out.println(String.valueOf(temp.getFarenheit());