简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM