자바 반사 메커니즘 은 개인 구조 방법 을 호출 하고 클래스 내 방법 을 실행 합 니 다.
1300 단어 자바
public class ModTest {
private ModTest(){
}
private static String testM(String str){
return "ok"+str;
}
}
어떻게 ModTest 류 중의 testM 방법 을 반사 적 으로 호출 합 니까?
모두 가 알 아야 할 것 은 단지 하나의 무 참 한 사유 구조 방법 만 으로 는 계승 할 수 없다 는 것 이다. 만약 에 그 중의 방법 을 호출 하려 면 다음 과 같은 코드 이다.
public static void main(String[] args) throws Exception { Class modTestClass = ModTest. class ; Constructor declaredConstructor = modTestClass.getDeclaredConstructor(); declaredConstructor.setAccessible( true ); Method method = modTestClass.getDeclaredMethod( "testM" , String. class ); method.setAccessible( true ); Object aa = method.invoke(declaredConstructor.newInstance(), " fuck me" ); System.out.println(aa); }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.