Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void test1(){};
- public void test2(){};
- public void test3(){};
- If (main.rand.nextInt(3) == x)
- package com.example;
- import java.util.Random;
- import java.lang.reflect.Method;
- class Test {
- public void test1() {}
- public void test2() {}
- public void test3() {}
- }
- public class Main {
- public static void main(String args[]) throws ReflectiveOperationException {
- Random rnd = new Random();
- String methodName = "test" + (rnd.nextInt(3) + 1);
- Class<?> cls = Class.forName("com.example.Test");
- Object obj = cls.newInstance();
- Method method = cls.getDeclaredMethod(methodName);
- method.invoke(obj);
- }
- }
- public class Test
- {
- public void test1() { System.out.println("1"); };
- public void test2() { System.out.println("2"); };
- public void test3() { System.out.println("3"); };
- public static void main(String[] args)
- {
- Test t = new Test();
- Runnable[] methods = { t::test1, t::test2, t::test3 };
- Random random = new Random();
- Runnable method = methods[random.nextInt(methods.length)];
- method.run();
- }
- }
Add Comment
Please, Sign In to add comment