Java枚举可以有行为? [英] Java Enums can have behavior?
本文介绍了Java枚举可以有行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
这是一个简单的例子:
枚举RoundingMode {
UP {
public double round(double d){
return Math.ceil(d);
}
},
DOWN {
public double round(double d){
return Math.floor(d);
}
};
public abstract double round(double d);
}
In Java, an Enum can do the great things that Enums do, but can also have methods (behavior and logic). What advantage does that have over using a class using an enum? Simple examples to illustrate the point would also be welcome.
解决方案
Here's a simple example:
enum RoundingMode {
UP {
public double round(double d) {
return Math.ceil(d);
}
},
DOWN {
public double round(double d) {
return Math.floor(d);
}
};
public abstract double round(double d);
}
这篇关于Java枚举可以有行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文