为什么在Java中,枚举在单独的文件中声明?
为什么此枚举在其自己的文件中声明。这有什么好处吗?另外,我如何能够将其放入2个文件之一中。我真的不知道我在做什么。也请用一个简单的方式来解释这一点,因为这是一个教科书的例子,而且我对Java还很陌生。
规模名称.java
enum ScaleName {celsius, fahrenheit, kelvin, rankine};
温度.java
class Temperature {
private double number;
private ScaleName scale;
public Temperature() {
number = 0.0;
scale = ScaleName.fahrenheit;
}
public Temperature(double number) {
this.number = number;
scale = ScaleName.fahrenheit;
}
public Temperature(ScaleName scale) {
number = 0.0;
this.scale = scale;
}
public Temperature(double number, ScaleName scale) {
this.number = number;
this.scale = scale;
}
public void setNumber(double number) {
this.number = number;
}
public double getNumber() {
return number;
}
public void setScale(ScaleName scale) {
this.scale = scale;
}
public ScaleName getScale() {
return scale;
}
}
使用温度.java
class Temperature {
private double number;
private ScaleName scale;
public Temperature() {
number = 0.0;
scale = ScaleName.fahrenheit;
}
public Temperature(double number) {
this.number = number;
scale = ScaleName.fahrenheit;
}
public Temperature(ScaleName scale) {
number = 0.0;
this.scale = scale;
}
public Temperature(double number, ScaleName scale) {
this.number = number;
this.scale = scale;
}
public void setNumber(double number) {
this.number = number;
}
public double getNumber() {
return number;
}
public void setScale(ScaleName scale) {
this.scale = scale;
}
public ScaleName getScale() {
return scale;
}
}