0%
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.zss.reflact;
public class Cat { public String name="asac"; public int age=0; private String sex="娜娜"; int tall=15; public void cry(){ System.out.println("HI"); } void hello(){ System.out.println("NIAO"); } @Override public String toString() { return "Cat{" + "name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + '}'; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package com.zss.reflact;
import java.util.Arrays;
public class Test { public static void main(String[] args) throws ClassNotFoundException { Class cla=Class.forName("com.zss.reflact.Cat"); System.out.println(cla.getName()); System.out.println(cla.getSimpleName()); System.out.println(Arrays.toString(cla.getFields())); System.out.println(Arrays.toString(cla.getDeclaredFields())); System.out.println(Arrays.toString(cla.getMethods())); System.out.println(Arrays.toString(cla.getDeclaredMethods())); System.out.println(Arrays.toString(cla.getConstructors())); System.out.println(Arrays.toString(cla.getDeclaredConstructors())); System.out.println(cla.getPackage()); System.out.println(cla.getSuperclass()); System.out.println(Arrays.toString(cla.getInterfaces()));
} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package com.zss.reflact;
import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Arrays;
public class Test { public static void main(String[] args) throws ClassNotFoundException { Class cla=Class.forName("com.zss.reflact.Cat"); Field[] fields= cla.getFields(); for (Field f:fields){ System.out.println(f.getName()+f.getModifiers()+f.getType()); }
Method[] methods= cla.getMethods(); for (Method m:methods){ System.out.println(m.getName()+m.getModifiers()+m.getReturnType()); }
Constructor []constructors= cla.getConstructors(); System.out.println(constructors[0].getName()+constructors[0].getModifiers()+ Arrays.toString(constructors[0].getTypeParameters())); } }
|