我想模拟枚举的所有实例的 shouldShow() 方法。有人可以帮忙指导一下如何做到这一点吗?
public enum MyButton implements ListButton {
Button1(
R.drawable.drawable1) {
// Some methods here
},
Button2(
R.drawable.drawable2) {
//Some methods here
};
@Override
public boolean shouldShow() {
//Some logic that decides whether to show the button or not
}
}
我想模拟它,这样如果我调用 Button1.shouldShow 或 Button2.shouldShow,或 enum.shouldShow 中的任何其他值,我的 stub 应该返回 true。
最佳答案
我认为 shouldShow()
方法是 ListButton
接口(interface)的一部分。如果您的设计经过深思熟虑,您很可能应该模拟 ListButton
,而不是任何特定 MyButton
所发生的情况。
Java 枚举常量本质上是公共(public)静态最终字段,因此模拟特定 MyButton
常量上的 shouldShow()
方法相当于模拟静态最终字段上的方法。
关于java - java 枚举的所有实例的模拟方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45290315/