Class实例化:
Class fzhlclass = Class.forName("fanshe_jiegou.Fzhl");
class
class A{
public int a1;
protected int a2;
int a3;
private int a4;
public int a1_;
protected int a2_;
int a3_;
private int a4_;
public A(){}
public A(String str){}
public void b1(){}
protected void b2(){}
void b3(){}
private void b4(){}
public void b1_(){}
protected void b2_(){}
void b3_(){}
private void b4_(){}
}
interface B{ }
interface C{}
class Fzhl extends A implements B,C{
public int a1;
protected int a2;
int a3;
private int a4;
public Fzhl(){}
public Fzhl(String str){}
private Fzhl(int a){}
public void b1(){}
protected void b2(){}
void b3(){}
private void b4(){}
}
获取名三种
// 获取全类名
System.out.println(fzhlclass.getName());
// 返回包名
System.out.println(fzhlclass.getPackage());
// 获取类名
System.out.println(fzhlclass.getSimpleName());
运行结果
获取属性信息
// 获取所有public 修饰的属性 包括父类
Field[] fields = fzhlclass.getFields();
System.out.println("==========");
for (Field field:fields){
System.out.println(field.getName());
}
System.out.println("==========");
// 获取本类的所有属性 去除父类
Field[] fields1 = fzhlclass.getDeclaredFields();
System.out.println("==========");
for (Field field:fields1){
System.out.println(field.getName());
}
运行结果
获取方法
// 获取所有public 修饰的方法 包括父类 父类也包括obj
Method[] methods = fzhlclass.getMethods();
System.out.println("----------");
for (Method method: methods){
System.out.println(method.getName());
}
System.out.println("----------");
// 获取本类的所有方法
Method[] methods1 = fzhlclass.getDeclaredMethods();
System.out.println("----------");
for (Method method: methods1){
System.out.println(method.getName());
}
System.out.println("----------");
运行结果
获取构造器
// 获取所有的public修饰的构造器 不包括父类
Constructor[] constructors = fzhlclass.getConstructors();
System.out.println("**********");
for (Constructor constructor:constructors){
System.out.println(constructor.getName());
}
System.out.println("**********");
// 获取本类所有的构造器
Constructor[] constructors1 = fzhlclass.getDeclaredConstructors();
System.out.println("**********");
for (Constructor constructor:constructors1){
System.out.println(constructor.getName());
}
System.out.println("**********");
运行结果:
获取父类或者接口Class
// 返回父类的 Class信息
Class superclass = fzhlclass.getSuperclass();
System.out.println(superclass.getName());
System.out.println(superclass.getSuperclass().getName());
// 返回接口的Class信息 以[]的形式接收
Class[] imclass = fzhlclass.getInterfaces();
System.out.println("&&&&&&&&&&&&");
for (Class c:imclass){
System.out.println(c.getName());
}
System.out.println("&&&&&&&&&");
运行结果:
修饰符的值
public 1 , priver 2 , 无 0 , protected 4 , static 8 , final 16 :
Class:
public int a1;
protected int a2;
int a3;
private int a4;
public static final int a5 = 0;
// 1 + 8 + 16 = 25
获取修饰值 + 属性类型
@Test
public void a() throws Exception{
Class fzhlclass = Class.forName("fanshe_jiegou.Fzhl");
// 获取本类的所有属性 去除父类
Field[] fields1 = fzhlclass.getDeclaredFields();
System.out.println("==========");
for (Field field:fields1){
System.out.println(field.getName()+"======="+
field.getModifiers()+field.getType());
}
}
运行结果
用到方法的修饰符一样,方法还可以获取返回类型+形参信息
Class:
public void b1(){}
protected int b2(int a){
return 0;
}
String b3(String a,String b){
return "";
}
private char b4(double a){
return ' ';
}
@Test
public void a2() throws Exception{
Class fzhlclass = Class.forName("fanshe_jiegou.Fzhl");
Method[] methods = fzhlclass.getDeclaredMethods();
for (Method method:methods){
System.out.println(method.getName()+"="+method.getModifiers()
+'='+method.getReturnType());
// 获取形参信息
Class[] cls = method.getParameterTypes();
for (Class c:cls){
System.out.println("该方法的参数为"+c+"="+c.getName());
}
}
}
运行结果
获取参数类型的方法可以直接c或c.getName
用到构造器写法也是一样
class
public Fzhl(){}
protected Fzhl(String str){}
private Fzhl(int a){}
Fzhl(char c){}
@Test
public void a3() throws Exception{
Class fzhlclass = Class.forName("fanshe_jiegou.Fzhl");
Constructor[] constructors = fzhlclass.getDeclaredConstructors();
for (Constructor constructor:constructors){
System.out.println(constructor.getName()+"="+
constructor.getModifiers());
Class[] cls = constructor.getParameterTypes();
for (Class c:cls){
System.out.println(c);
}
}
}