import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args)
throws NoSuchMethodException,InvocationTargetException, IllegalAccessException {
Calculator calculator = new Calculator();
Integer returnValue = Action.run(
1,
Integer.class,
calculator,
Calculator.class.getDeclaredMethod(
"plus",
Integer.class,
Integer.class
),
5,
10);
System.out.println("returnValue : " + returnValue);
Integer staticReturnValue = Action.run(
1,
Integer.class,
null,
Calculator.class.getDeclaredMethod(
"minus",
Integer.class,
Integer.class),
5,
10);
System.out.println("staticReturnValue : " + staticReturnValue);
}
static class Calculator {
public Integer plus(Integer a, Integer b) {
System.out.println(a + " + " + b + " = " + (a + b));
return a + b;
}
public static Integer minus(Integer a, Integer b) {
System.out.println(a + " - " + b + " = " + (a - b));
return a - b;
}
}
static class Action {
public static <T> T run(
double percentage,
Class<T> returnClass,
Object object,
Method method,
Object... args)
throws InvocationTargetException, IllegalAccessException {
double random = Math.random();
if (random < percentage) {
return (T) method.invoke(object, args);
}
return null;
}
}
}
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args)
throws NoSuchMethodException,InvocationTargetException, IllegalAccessException {
Calculator calculator = new Calculator();
Integer returnValue = Action.run(
1,
Integer.class,
calculator,
Calculator.class.getDeclaredMethod(
"plus",
Integer.class,
Integer.class
),
5,
10);
System.out.println("returnValue : " + returnValue);
Integer staticReturnValue = Action.run(
1,
Integer.class,
null,
Calculator.class.getDeclaredMethod(
"minus",
Integer.class,
Integer.class),
5,
10);
System.out.println("staticReturnValue : " + staticReturnValue);
}
static class Calculator {
public Integer plus(Integer a, Integer b) {
System.out.println(a + " + " + b + " = " + (a + b));
return a + b;
}
public static Integer minus(Integer a, Integer b) {
System.out.println(a + " - " + b + " = " + (a - b));
return a - b;
}
}
static class Action {
public static <T> T run(
double percentage,
Class<T> returnClass,
Object object,
Method method,
Object... args)
throws InvocationTargetException, IllegalAccessException {
double random = Math.random();
if (random < percentage) {
return (T) method.invoke(object, args);
}
return null;
}
}
}
대충 확률로 메서드 실행 시켜주는 건데
생각해보니까 그냥 메서드마다 확률로 실행시켜주는 걸 넣는 게 나을 듯 싶긴한 데 몰루겠다 ㅇㅇㅇㅇㅇㅇㅇㅇㅇㅇㅇㅇ
근데 저렇게 복잡하게 안짜도 되는거 아님?
만들고나니까 로직 단에서 확률 받아서 그냥 하면 될 것 같아 만들고 보니까 리턴도 그냥 페어 클래스 하나 만들어서 <값, 실패(boolean)> 이게 더 나은 것 같고
넘기는 거 자체를 확률적으로 만드셈 - dc App
ㄹㅇ 그냥 매개변수 하나만 더 받으면 되는 걸 이렇게 만들 이유가 없다고 느끼고 현타오는 주임 ㅜ