在Java中随机选择函数

时间:2019-02-20 19:26:21

标签: java

我想随机选择一个功能。这是下面的代码:

public class Thank_you_five implements RandomInterface
{
  public static void Thankyou_five(){...}
}
public class Thank_you_four implements RandomInterface
{
  public static void Thankyou_four(){...}
}
public class Thank_you_three implements RandomInterface
{
  public static void Thankyou_three(){...}
}
public interface RandomInterface
{
  public static void Thankyou_five();
  public static void Thankyou_four();
  public static void Thankyou_three();
}

所以我的目标是随机选择一个函数,就像在python中我们选择random.choose()和其中的一些函数,我想用Java实现相同的功能

请帮助。

谢谢, 阿德里亚

2 个答案:

答案 0 :(得分:3)

首先,最好在接口内部定义一个abstarct方法:

public class FieldDefinitions {
    @JsonProperty("Fields")
    public FieldDetails fields = new FieldDetails();

    @JsonProperty("Description")
    public Map<String, Property> properties = new HashMap<>();
}

public class FieldDetails {
    @JsonProperty("Required")
    public List<String> required = new ArrayList<>();

    @JsonProperty("Optional")
    public List<String> optional = new ArrayList<>();
}

public class Property {
    public String label;
    public String description;
}

然后您可以创建几个实现:

public interface RandomInterface{
    void thankYou();
}

要获得随机实现,可以将所有对象添加到数组并生成随机索引:

RandomInterface t1 = () -> System.out.println("thank you 1");
RandomInterface t2 = () -> System.out.println("thank you 2");
RandomInterface t3 = () -> System.out.println("thank you 3");

答案 1 :(得分:2)

坦率地说,我更喜欢Ruslan的建议。但是只要您问,这就是我的想法:

package com.example;

import java.util.Random;

public abstract class RandomFn {

    public static RandomFn factory() throws Exception {
        int r = new Random().nextInt(3);
        switch (r) {
           case 0: return new ThankYouOne();
           case 1: return new ThankYouTwo();
           case 2: return new ThankYouThree();
           default : throw new Exception ("ThankYou(" + r +"): unsupported value!");
        }
    }

    public abstract void thankYou();

    public static void main(String[] args) {
        try {
            RandomFn.factory().thankYou();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

}

class ThankYouOne extends RandomFn {
    public void thankYou() {
        System.out.println("Thank you one");
    }
}

class ThankYouTwo extends RandomFn {
    public void thankYou() {
        System.out.println("Thank you two");
    }
}

class ThankYouThree extends RandomFn {
    public void thankYou() {
        System.out.println("Thank you three");
    }
}

如果您不喜欢许多类的想法,那么

public class RandomFn {

    public void thankYou () throws Exception {
        int r = new Random().nextInt(3);
        switch (r) {
           case 0: 
             thankYouOne(); break;
           case 1: 
             thankYouTwo(); break;
           case 2: 
             thankYouThree(); break;
           default : 
             throw new Exception ("ThankYou(" + r +"): unsupported value!");
        }
    }

    private void thankYouOne() { System.out.println("Thank you one"); }

    private void thankYouTwo() { System.out.println("Thank you two"); }

    private void thankYouThree() { System.out.println("Thank you three"); }
    ...