Guest User

Untitled

a guest
Oct 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. public void test1(){};
  2. public void test2(){};
  3. public void test3(){};
  4.  
  5. If (main.rand.nextInt(3) == x)
  6.  
  7. package com.example;
  8.  
  9. import java.util.Random;
  10. import java.lang.reflect.Method;
  11.  
  12. class Test {
  13. public void test1() {}
  14. public void test2() {}
  15. public void test3() {}
  16. }
  17.  
  18. public class Main {
  19. public static void main(String args[]) throws ReflectiveOperationException {
  20. Random rnd = new Random();
  21.  
  22. String methodName = "test" + (rnd.nextInt(3) + 1);
  23.  
  24. Class<?> cls = Class.forName("com.example.Test");
  25. Object obj = cls.newInstance();
  26.  
  27. Method method = cls.getDeclaredMethod(methodName);
  28. method.invoke(obj);
  29. }
  30. }
  31.  
  32. public class Test
  33. {
  34. public void test1() { System.out.println("1"); };
  35. public void test2() { System.out.println("2"); };
  36. public void test3() { System.out.println("3"); };
  37.  
  38. public static void main(String[] args)
  39. {
  40. Test t = new Test();
  41. Runnable[] methods = { t::test1, t::test2, t::test3 };
  42. Random random = new Random();
  43. Runnable method = methods[random.nextInt(methods.length)];
  44. method.run();
  45. }
  46. }
Add Comment
Please, Sign In to add comment