JAVA에서 리플렉션을 사용하여 다른 클래스의 클래스 함수를 호출하는 방법은 무엇입니까?

인치
import org.apache.commons.beanutils.MethodUtils;
public class subscriber
{
    public calculator cal=new calculator();
    String funcName = "";
    int result;
    public void getValues(String strfuncName)
    {
            funcName= strfuncName; //has the function name of class calculator

    }

    public void giveResult()
    {
            result=cal.funcName();
     //i want it to call the function whose name is stored in funcName
    }
}

리플렉션을 사용하고 org.apache.commons.beanutils.MethodUtils
가져 오기를 사용하는 방법 그리고 함수 호출이 동적 일 수 있으므로 함수를 호출 할 수 있으므로 미리 정의 할 수 없습니다.

카르티 게얀 벨라 사미
Method method = obj.getClass().getMethod("Methodname", new Class[] {});
String output = (String) method.invoke(obj, new Object[] {}); // String return type here, Sorry i mean yourType is your return type..

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관