클래스 내에서 메소드를 표시하는 프로그램을 액세스 수정 자, 반환 유형 및 매개 변수와 함께 작성하고 있습니다.getTypeParameters는 null TypeVariable 배열을 반환합니다.
여기에 Test.java
class Test{
private int x;
public double y;
protected String z;
static long a;
public Test(){
x = 10;
y = 20;
z = "Hello";
a = 15L;
}
public void Print(String a){
a = a;
System.out.println("Executing Print function.");
}
private void hidden(double b){
b = b;
//private function
}
}
모든 일이 잘 작동하지만 난 줄 TypeVariable
의 빈 배열을 얻을 왜 이해가 안 내 코드
import java.lang.reflect.*;
class RefTest1{
public static void main(String[] args) throws Exception{
Test obj = new Test();
Class<?> c = obj.getClass();
System.out.printf("%n%s fields :-%n", obj.getClass());
Field[] fields = c.getDeclaredFields();
for(Field f : fields){
f.setAccessible(true);
int m = f.getModifiers();
if(Modifier.isStatic(m)){
System.out.printf("%s is static variable and its value is %s%n", f.getName(), f.get(obj));
}else if(Modifier.isPublic(m)){
System.out.printf("%s is public variable and its value is %s%n", f.getName(), f.get(obj));
}else if(Modifier.isPrivate(m)){
System.out.printf("%s is private variable and its value is %s%n", f.getName(), f.get(obj));
}else if(Modifier.isProtected(m)){
System.out.printf("%s is protected variable and its value is %s%n", f.getName(), f.get(obj));
}
}
System.out.printf("%n%s methods :-%n", obj.getClass());
Method[] methods = c.getDeclaredMethods();
for(Method meth : methods){
int m = meth.getModifiers();
meth.setAccessible(true);
if(Modifier.isStatic(m)){
System.out.printf("%s is static method%n", meth.getName());
}else if(Modifier.isPublic(m)){
System.out.printf("%s is public method%n", meth.getName());
}else if(Modifier.isPrivate(m)){
System.out.printf("%s is private method%n", meth.getName());
}else if(Modifier.isProtected(m)){
System.out.printf("%s is protected method%n", meth.getName());
}
System.out.printf("%nReturn Type :- %s%n", meth.getReturnType());
System.out.printf("%nParameters:-%n");
TypeVariable[] parameters = meth.getTypeParameters();
for(TypeVariable param : parameters){
System.out.printf("%s", param.getName());
}
}
System.out.println();
}
}
의 TypeVariable[] parameters = meth.getTypeParameters();
어떤 사람이 올바른 방향으로 나를 가리킬 수 있습니까?
감사합니다.
반환
getParameterTypes()
를 사용한다고 생각합니다. 편집을 참조하십시오, 내 코드에 오류가 있다고 생각합니다. – Searock@Searock : 변경 사항을 원래 코드에 통합 한 후 매개 변수 유형을 인쇄합니다. 그러나 유형 다음에 개행 문자를 인쇄하지 않으므로 놓치기 쉽습니다. –
정말 미안 해요, 제 잘못입니다. 어딘가에 매개 변수 유형을 알지 못했던 개행 문제가 있습니다. 빠른 응답을 보내 주셔서 감사합니다. – Searock