[英]Method with restrict parameter
When you try to give another value to the 3rd argument in Toast.makeText
other than Toast.LENGTH_LONG
or Toast.LENGTH_SHORT
it won't accept. 当你试图给另一个值在第三个参数
Toast.makeText
比其他Toast.LENGTH_LONG
或Toast.LENGTH_SHORT
它不会接受。
I need to create a method and use the same type of restriction: it can only be accepted three values which I'll define previously using constants. 我需要创建一个方法并使用相同类型的限制:它只能被接受三个我先前使用常量定义的值。
How can this be accomplished? 如何实现这一目标?
You can restrict the user by creating your own enum. 您可以通过创建自己的枚举来限制用户。 Sample code:
示例代码:
public enum Notification {
LONG,
SHORT
}
Method which I want to restrict: 我想限制的方法:
private void methodName(String value1, Notification value2){
// Write whatever you want to perform
}
Now, this method can only be called like: 现在,这个方法只能被调用为:
methodName("", Notification.LONG); // Second Parameter is allowed only from your custom enum
如果传递的参数不是您想要的,则从您的方法中抛出IllegalArgumentException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.